file = $file; $this->option_prefix = 'siti_updater_' . sanitize_key( dirname( plugin_basename( $this->file ) ) ) . '_'; $this->set_plugin_properties(); add_action( 'admin_init', array( $this, 'set_plugin_properties' ) ); add_action( 'admin_notices', array( $this, 'admin_notices' ) ); add_action( 'siti_updater_daily_check_' . $this->option_prefix, array( $this, 'daily_license_check' ) ); if ( ! wp_next_scheduled( 'siti_updater_daily_check_' . $this->option_prefix ) ) { wp_schedule_event( time(), 'daily', 'siti_updater_daily_check_' . $this->option_prefix ); } return $this; } public function initialize() { add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_updates' ) ); add_filter( 'plugins_api', array( $this, 'plugin_info' ), 10, 3 ); add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); } public function set_plugin_properties() { if ( ! function_exists( 'get_plugin_data' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $this->plugin = get_plugin_data( $this->file, false, false ); $this->basename = plugin_basename( $this->file ); $this->active = is_plugin_active( $this->basename ); } public function set_owner( $owner ) { $this->owner = $owner; } public function set_repository( $repository ) { $this->repository = $repository; } public function set_license_key( $key ) { $this->license_key = $key; update_option( $this->option_prefix . 'license_key', $key ); } public function get_license_key() { if ( null === $this->license_key ) { $this->license_key = get_option( $this->option_prefix . 'license_key', '' ); } return $this->license_key; } public function set_api_base_url( $api_base_url ) { $this->api_base_url = rtrim( (string) $api_base_url, '/' ); } private function get_plugin_info() { if ( is_null( $this->plugin_response ) ) { $request_uri = sprintf( '%s/api/plugins/%s/%s', $this->api_base_url, $this->owner, $this->repository ); $response = wp_remote_get( $request_uri, array( 'timeout' => 15 ) ); if ( is_wp_error( $response ) ) { $this->plugin_response = false; return; } $code = wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { $this->plugin_response = false; return; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); $this->plugin_response = $body; } } public function check_for_updates( $transient ) { if ( empty( $transient->checked ) ) { return $transient; } $this->get_plugin_info(); if ( false === $this->plugin_response || empty( $this->plugin_response['version'] ) ) { return $transient; } $remote_version = $this->plugin_response['version']; $current_version = $this->plugin['Version']; if ( version_compare( $remote_version, $current_version, '>' ) ) { $download_url = $this->get_download_url( 'latest' ); if ( ! is_wp_error( $download_url ) ) { $transient->response[ $this->basename ] = (object) array( 'slug' => dirname( $this->basename ), 'new_version' => $remote_version, 'package' => $download_url, 'tested' => get_bloginfo( 'version' ), 'url' => $this->plugin_response['homepage'] ?? '', ); } } return $transient; } public function plugin_info( $result, $action, $args ) { if ( 'plugin_information' !== $action || $args->slug !== dirname( $this->basename ) ) { return $result; } $this->get_plugin_info(); if ( false === $this->plugin_response ) { return $result; } return (object) array( 'name' => $this->plugin_response['name'] ?? $this->plugin['Name'], 'slug' => $args->slug, 'version' => $this->plugin_response['version'], 'author' => $this->plugin_response['author'] ?? $this->plugin['Author'], 'author_profile' => $this->plugin_response['author_url'] ?? $this->plugin['AuthorURI'], 'homepage' => $this->plugin_response['homepage'] ?? '', 'requires' => $this->plugin_response['requires'] ?? '', 'tested' => $this->plugin_response['tested'] ?? get_bloginfo( 'version' ), 'downloaded' => $this->plugin_response['downloads'] ?? 0, 'last_updated' => $this->plugin_response['last_updated'] ?? '', 'sections' => array( 'description' => $this->plugin_response['description'] ?? $this->plugin['Description'], 'changelog' => $this->plugin_response['changelog'] ?? '', ), 'download_link' => $this->get_download_url( 'latest' ), ); } private function get_download_url( $version = 'latest' ) { $license_key = $this->get_license_key(); if ( empty( $license_key ) ) { return new WP_Error( 'no_license', 'Geen licentie ingesteld' ); } $hostname = parse_url( home_url(), PHP_URL_HOST ); return $this->api_base_url . '/api/licenses/download?key=' . urlencode( $license_key ) . '&hostname=' . urlencode( $hostname ) . '&version=' . urlencode( $version ); } public function verify_license() { $license_key = $this->get_license_key(); if ( empty( $license_key ) ) { return new WP_Error( 'missing_license', 'Geen licentiecode ingesteld.' ); } $hostname = parse_url( home_url(), PHP_URL_HOST ); $response = wp_remote_post( $this->api_base_url . '/api/licenses/verify', array( 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'key' => $license_key, 'hostname' => $hostname, ) ), 'timeout' => 15, ) ); if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( 200 !== $code ) { $error_message = isset( $body['error'] ) ? $body['error'] : 'Licentie verificatie mislukt.'; return new WP_Error( 'verification_failed', $error_message ); } if ( empty( $body['valid'] ) ) { return new WP_Error( 'invalid_license', 'Licentie is ongeldig.' ); } // Store license data update_option( $this->option_prefix . 'license_data', $body['license'] ); update_option( $this->option_prefix . 'last_check', current_time( 'mysql' ) ); return $body; } public function is_license_valid() { $data = get_option( $this->option_prefix . 'license_data', array() ); return ! empty( $data ) && isset( $data['key'] ); } public function admin_notices() { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( ! $this->is_license_valid() ) { $settings_url = admin_url( 'options-general.php?page=siti-updater-' . sanitize_title( $this->plugin['Name'] ) ); echo '
'; printf( esc_html__( '%s: Licentie ongeldig of niet ingesteld. Ga naar %s om je licentie in te voeren.', 'siti-updater' ), esc_html( $this->plugin['Name'] ), '' . esc_html__( 'instellingen', 'siti-updater' ) . '' ); echo '
plugin['Name'] ); ?>