diff --git a/src/Insights.php b/src/Insights.php index 8187d88..a86d19a 100644 --- a/src/Insights.php +++ b/src/Insights.php @@ -9,8 +9,7 @@ * No personal information is being tracked by this class, only general settings, active plugins, environment details * and admin email. */ -class Insights -{ +class Insights { /** * The notice text @@ -20,7 +19,7 @@ class Insights public $notice; /** - * Wheather to the notice or not + * Whether to show the notice or not * * @var bool */ @@ -31,7 +30,7 @@ class Insights * * @var array */ - protected $extra_data = []; + protected $extra_data = array(); /** * AppSero\Client @@ -41,6 +40,8 @@ class Insights protected $client; /** + * Whether to include plugin data + * * @var bool */ private $plugin_data = false; @@ -48,16 +49,16 @@ class Insights /** * Initialize the class * - * @param null $name - * @param null $file + * @param mixed $client Client object or string. + * @param string $name Name of the plugin/theme. + * @param string $file Main plugin file path. */ - public function __construct($client, $name = null, $file = null) - { - if (is_string($client) && !empty($name) && !empty($file)) { - $client = new Client($client, $name, $file); + public function __construct( $client, $name = null, $file = null ) { + if ( is_string( $client ) && ! empty( $name ) && ! empty( $file ) ) { + $client = new Client( $client, $name, $file ); } - if (is_object($client) && is_a($client, 'Appsero\Client')) { + if ( is_object( $client ) && is_a( $client, 'Appsero\Client' ) ) { $this->client = $client; } } @@ -65,10 +66,9 @@ public function __construct($client, $name = null, $file = null) /** * Don't show the notice * - * @return \self + * @return self */ - public function hide_notice() - { + public function hide_notice() { $this->show_notice = false; return $this; @@ -77,10 +77,9 @@ public function hide_notice() /** * Add plugin data if needed * - * @return \self + * @return self */ - public function add_plugin_data() - { + public function add_plugin_data() { $this->plugin_data = true; return $this; @@ -89,12 +88,11 @@ public function add_plugin_data() /** * Add extra data if needed * - * @param array $data + * @param array $data Extra data. * - * @return \self + * @return self */ - public function add_extra($data = []) - { + public function add_extra( $data = array() ) { $this->extra_data = $data; return $this; @@ -103,12 +101,11 @@ public function add_extra($data = []) /** * Set custom notice text * - * @param string $text + * @param string $text Custom notice text. * - * @return \self + * @return self */ - public function notice($text = '') - { + public function notice( $text = '' ) { $this->notice = $text; return $this; @@ -119,11 +116,10 @@ public function notice($text = '') * * @return void */ - public function init() - { - if ($this->client->type === 'plugin') { + public function init() { + if ( 'plugin' === $this->client->type ) { $this->init_plugin(); - } elseif ($this->client->type === 'theme') { + } elseif ( 'theme' === $this->client->type ) { $this->init_theme(); } } @@ -133,12 +129,11 @@ public function init() * * @return void */ - public function init_theme() - { + public function init_theme() { $this->init_common(); - add_action('switch_theme', [$this, 'deactivation_cleanup']); - add_action('switch_theme', [$this, 'theme_deactivated'], 12, 3); + add_action( 'switch_theme', array( $this, 'deactivation_cleanup' ) ); + add_action( 'switch_theme', array( $this, 'theme_deactivated' ), 12, 3 ); } /** @@ -146,21 +141,14 @@ public function init_theme() * * @return void */ - public function init_plugin() - { - // plugin deactivate popup - // if ( ! $this->is_local_server() ) { - // add_filter( 'plugin_action_links_' . $this->client->basename, [ $this, 'plugin_action_links' ] ); - // add_action( 'admin_footer', [ $this, 'deactivate_scripts' ] ); - // } - - add_filter('plugin_action_links_' . $this->client->basename, [$this, 'plugin_action_links']); - add_action('admin_footer', [$this, 'deactivate_scripts']); + public function init_plugin() { + add_filter( 'plugin_action_links_' . $this->client->basename, array( $this, 'plugin_action_links' ) ); + add_action( 'admin_footer', array( $this, 'deactivate_scripts' ) ); $this->init_common(); - register_activation_hook($this->client->file, [$this, 'activate_plugin']); - register_deactivation_hook($this->client->file, [$this, 'deactivation_cleanup']); + register_activation_hook( $this->client->file, array( $this, 'activate_plugin' ) ); + register_deactivation_hook( $this->client->file, array( $this, 'deactivation_cleanup' ) ); } /** @@ -168,49 +156,43 @@ public function init_plugin() * * @return void */ - protected function init_common() - { - if ($this->show_notice) { - // tracking notice - add_action('admin_notices', [$this, 'admin_notice']); + protected function init_common() { + if ( $this->show_notice ) { + add_action( 'admin_notices', array( $this, 'admin_notice' ) ); } - add_action('admin_init', [$this, 'handle_optin_optout']); + add_action( 'admin_init', array( $this, 'handle_optin_optout' ) ); - // uninstall reason - add_action('wp_ajax_' . $this->client->slug . '_submit-uninstall-reason', [$this, 'uninstall_reason_submission']); + add_action( 'wp_ajax_' . $this->client->slug . '_submit-uninstall-reason', array( $this, 'uninstall_reason_submission' ) ); - // cron events - add_filter('cron_schedules', [$this, 'add_weekly_schedule']); - add_action($this->client->slug . '_tracker_send_event', [$this, 'send_tracking_data']); - // add_action( 'admin_init', array( $this, 'send_tracking_data' ) ); // test + add_filter( 'cron_schedules', array( $this, 'add_weekly_schedule' ) ); + add_action( $this->client->slug . '_tracker_send_event', array( $this, 'send_tracking_data' ) ); } /** * Send tracking data to AppSero server * - * @param bool $override + * @param bool $override Whether to override the tracking allowed check. * * @return void */ - public function send_tracking_data($override = false) - { - if (!$this->tracking_allowed() && !$override) { + public function send_tracking_data( $override = false ) { + if ( ! $this->tracking_allowed() && ! $override ) { return; } - // Send a maximum of once per week + // Send a maximum of once per week. $last_send = $this->get_last_send(); - if ($last_send && $last_send > strtotime('-1 week')) { + if ( $last_send && $last_send > strtotime( '-1 week' ) ) { return; } $tracking_data = $this->get_tracking_data(); - $response = $this->client->send_request($tracking_data, 'track'); + $response = $this->client->send_request( $tracking_data, 'track' ); - update_option($this->client->slug . '_tracking_last_send', time()); + update_option( $this->client->slug . '_tracking_last_send', time() ); } /** @@ -218,88 +200,87 @@ public function send_tracking_data($override = false) * * @return array */ - protected function get_tracking_data() - { + protected function get_tracking_data() { $all_plugins = $this->get_all_plugins(); $users = get_users( - [ + array( 'role' => 'administrator', 'orderby' => 'ID', 'order' => 'ASC', 'number' => 1, 'paged' => 1, - ] + ) ); - $admin_user = (is_array($users) && !empty($users)) ? $users[0] : false; + $admin_user = ( is_array( $users ) && ! empty( $users ) ) ? $users[0] : false; $first_name = ''; $last_name = ''; - if ($admin_user) { + if ( $admin_user ) { $first_name = $admin_user->first_name ? $admin_user->first_name : $admin_user->display_name; $last_name = $admin_user->last_name; } - $data = [ - 'url' => esc_url(home_url()), + $data = array( + 'url' => esc_url( home_url() ), 'site' => $this->get_site_name(), - 'admin_email' => get_option('admin_email'), + 'admin_email' => get_option( 'admin_email' ), 'first_name' => $first_name, 'last_name' => $last_name, 'hash' => $this->client->hash, 'server' => $this->get_server_info(), 'wp' => $this->get_wp_info(), 'users' => $this->get_user_counts(), - 'active_plugins' => count($all_plugins['active_plugins']), - 'inactive_plugins' => count($all_plugins['inactive_plugins']), + 'active_plugins' => count( $all_plugins['active_plugins'] ), + 'inactive_plugins' => count( $all_plugins['inactive_plugins'] ), 'ip_address' => $this->get_user_ip_address(), 'project_version' => $this->client->project_version, 'tracking_skipped' => false, 'is_local' => $this->is_local_server(), - ]; + ); - // Add Plugins - if ($this->plugin_data) { - $plugins_data = []; + // Add Plugins. + if ( $this->plugin_data ) { + $plugins_data = array(); - foreach ($all_plugins['active_plugins'] as $slug => $plugin) { - $slug = strstr($slug, '/', true); + foreach ( $all_plugins['active_plugins'] as $slug => $plugin ) { + $slug = strstr( $slug, '/', true ); - if (!$slug) { + if ( ! $slug ) { continue; } - $plugins_data[$slug] = [ - 'name' => isset($plugin['name']) ? $plugin['name'] : '', - 'version' => isset($plugin['version']) ? $plugin['version'] : '', - ]; + $plugins_data[ $slug ] = array( + 'name' => isset( $plugin['name'] ) ? $plugin['name'] : '', + 'version' => isset( $plugin['version'] ) ? $plugin['version'] : '', + ); } - if (array_key_exists($this->client->slug, $plugins_data)) { - unset($plugins_data[$this->client->slug]); + if ( array_key_exists( $this->client->slug, $plugins_data ) ) { + unset( $plugins_data[ $this->client->slug ] ); } $data['plugins'] = $plugins_data; } - // Add Metadata + // Add Metadata. $extra = $this->get_extra_data(); - if ($extra) { + if ( $extra ) { $data['extra'] = $extra; } - // Check this has previously skipped tracking - $skipped = get_option($this->client->slug . '_tracking_skipped'); + // Check if tracking was previously skipped. + $skipped = get_option( $this->client->slug . '_tracking_skipped' ); - if ($skipped === 'yes') { - delete_option($this->client->slug . '_tracking_skipped'); + if ( 'yes' === $skipped ) { + delete_option( $this->client->slug . '_tracking_skipped' ); $data['tracking_skipped'] = true; } - return apply_filters($this->client->slug . '_tracker_data', $data); + return apply_filters( $this->client->slug . '_tracker_data', $data ); } /** @@ -307,17 +288,16 @@ protected function get_tracking_data() * * @return mixed */ - protected function get_extra_data() - { - if (is_callable($this->extra_data)) { - return call_user_func($this->extra_data); + protected function get_extra_data() { + if ( is_callable( $this->extra_data ) ) { + return call_user_func( $this->extra_data ); } - if (is_array($this->extra_data)) { + if ( is_array( $this->extra_data ) ) { return $this->extra_data; } - return []; + return array(); } /** @@ -325,19 +305,18 @@ protected function get_extra_data() * * @return array */ - protected function data_we_collect() - { - $data = [ + protected function data_we_collect() { + $data = array( 'Server environment details (php, mysql, server, WordPress versions)', 'Number of users in your site', 'Site language', 'Number of active and inactive plugins', 'Site name and URL', 'Your name and email address', - ]; + ); - if ($this->plugin_data) { - array_splice($data, 4, 0, ["active plugins' name"]); + if ( $this->plugin_data ) { + array_splice( $data, 4, 0, array( "active plugins' name" ) ); } return $data; @@ -348,11 +327,10 @@ protected function data_we_collect() * * @return bool */ - public function tracking_allowed() - { - $allow_tracking = get_option($this->client->slug . '_allow_tracking', 'no'); + public function tracking_allowed() { + $allow_tracking = get_option( $this->client->slug . '_allow_tracking', 'no' ); - return $allow_tracking === 'yes'; + return 'yes' === $allow_tracking; } /** @@ -360,9 +338,8 @@ public function tracking_allowed() * * @return false|string */ - private function get_last_send() - { - return get_option($this->client->slug . '_tracking_last_send', false); + private function get_last_send() { + return get_option( $this->client->slug . '_tracking_last_send', false ); } /** @@ -370,11 +347,10 @@ private function get_last_send() * * @return bool */ - public function notice_dismissed() - { - $hide_notice = get_option($this->client->slug . '_tracking_notice', null); + public function notice_dismissed() { + $hide_notice = get_option( $this->client->slug . '_tracking_notice', null ); - if ('hide' === $hide_notice) { + if ( 'hide' === $hide_notice ) { return true; } @@ -386,21 +362,20 @@ public function notice_dismissed() * * @return bool */ - private function is_local_server() - { - $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : 'localhost'; - $ip = isset($_SERVER['SERVER_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_ADDR'])) : '127.0.0.1'; - $is_local = false; + private function is_local_server() { + $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : 'localhost'; + $ip = isset( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) ) : '127.0.0.1'; + $is_local = false; if ( - in_array($ip, ['127.0.0.1', '::1'], true) - || !strpos($host, '.') - || in_array(strrchr($host, '.'), ['.test', '.testing', '.local', '.localhost', '.localdomain'], true) + in_array( $ip, array( '127.0.0.1', '::1' ), true ) || + ! strpos( $host, '.' ) || + in_array( strrchr( $host, '.' ), array( '.test', '.testing', '.local', '.localhost', '.localdomain' ), true ) ) { $is_local = true; } - return apply_filters('appsero_is_local', $is_local); + return apply_filters( 'appsero_is_local', $is_local ); } /** @@ -408,12 +383,11 @@ private function is_local_server() * * @return void */ - private function schedule_event() - { - $hook_name = wp_unslash($this->client->slug . '_tracker_send_event'); + private function schedule_event() { + $hook_name = wp_unslash( $this->client->slug . '_tracker_send_event' ); - if (!wp_next_scheduled($hook_name)) { - wp_schedule_event(time(), 'weekly', $hook_name); + if ( ! wp_next_scheduled( $hook_name ) ) { + wp_schedule_event( time(), 'weekly', $hook_name ); } } @@ -422,9 +396,8 @@ private function schedule_event() * * @return void */ - private function clear_schedule_event() - { - wp_clear_scheduled_hook($this->client->slug . '_tracker_send_event'); + private function clear_schedule_event() { + wp_clear_scheduled_hook( $this->client->slug . '_tracker_send_event' ); } /** @@ -432,53 +405,49 @@ private function clear_schedule_event() * * @return void */ - public function admin_notice() - { - if ($this->notice_dismissed()) { + public function admin_notice() { + if ( $this->notice_dismissed() ) { return; } - if ($this->tracking_allowed()) { + if ( $this->tracking_allowed() ) { return; } - if (!current_user_can('manage_options')) { + if ( ! current_user_can( 'manage_options' ) ) { return; } - // don't show tracking if a local server - // if ( $this->is_local_server() ) { - // return; - // } + $optin_url = wp_nonce_url( add_query_arg( $this->client->slug . '_tracker_optin', 'true' ), '_wpnonce' ); + $optout_url = wp_nonce_url( add_query_arg( $this->client->slug . '_tracker_optout', 'true' ), '_wpnonce' ); - $optin_url = wp_nonce_url(add_query_arg($this->client->slug . '_tracker_optin', 'true'), '_wpnonce'); - $optout_url = wp_nonce_url(add_query_arg($this->client->slug . '_tracker_optout', 'true'), '_wpnonce'); - - if (empty($this->notice)) { - $notice = sprintf($this->client->__trans('Want to help make %1$s even more awesome? Allow %1$s to collect diagnostic data and usage information.'), $this->client->name); + if ( empty( $this->notice ) ) { + $notice = sprintf( + $this->client->__trans( 'Want to help make %1$s even more awesome? Allow %1$s to collect diagnostic data and usage information.' ), + $this->client->name + ); } else { $notice = $this->notice; } $policy_url = 'https://appsero.com/privacy-policy/'; - $notice .= ' (' . $this->client->__trans('what we collect') . ')'; - $notice .= ''; echo '

'; - echo $notice; + echo wp_kses_post( $notice ); echo '

'; - echo ' ' . $this->client->__trans('Allow') . ''; - echo ' ' . $this->client->__trans('No thanks') . ''; + echo ' ' . esc_html( $this->client->__trans( 'Allow' ) ) . ''; + echo ' ' . esc_html( $this->client->__trans( 'No thanks' ) ) . ''; echo '

'; - echo " - "; + "; } /** @@ -486,33 +455,95 @@ public function admin_notice() * * @return void */ - public function handle_optin_optout() - { - if (!isset($_GET['_wpnonce'])) { + public function handle_optin_optout() { + if ( ! $this->is_valid_request() || ! $this->has_manage_options_capability() ) { return; } - if (!wp_verify_nonce(sanitize_key($_GET['_wpnonce']), '_wpnonce')) { - return; + if ( $this->is_optin_request() ) { + $this->optin(); + $this->handle_redirection( $this->client->slug . '_tracker_optin' ); } - if (!current_user_can('manage_options')) { - return; + if ( $this->is_optout_request() ) { + $this->optout(); + $this->handle_redirection( $this->client->slug . '_tracker_optout' ); } + } - if (isset($_GET[$this->client->slug . '_tracker_optin']) && $_GET[$this->client->slug . '_tracker_optin'] === 'true') { - $this->optin(); + /** + * Validate the request nonce. + * + * @return bool + */ + private function is_valid_request() { + return isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), '_wpnonce' ); + } + + /** + * Check if the current user has manage options capability. + * + * @return bool + */ + private function has_manage_options_capability() { + return current_user_can( 'manage_options' ); + } + + /** + * Check if the current request is for opt-in. + * + * @return bool + */ + private function is_optin_request() { + return isset( $_GET[ $this->client->slug . '_tracker_optin' ] ) && 'true' === $_GET[ $this->client->slug . '_tracker_optin' ]; + } + + /** + * Check if the current request is for opt-out. + * + * @return bool + */ + private function is_optout_request() { + return isset( $_GET[ $this->client->slug . '_tracker_optout' ] ) && 'true' === $_GET[ $this->client->slug . '_tracker_optout' ]; + } - wp_safe_redirect(remove_query_arg($this->client->slug . '_tracker_optin')); - exit; + /** + * Handle redirection after opt-in/opt-out actions. + * + * @param string $param The query parameter to remove. + */ + private function handle_redirection( $param ) { + if ( $this->is_inaccessible_page() ) { + wp_safe_redirect( admin_url() ); + } else { + wp_safe_redirect( remove_query_arg( $param ) ); } + exit; + } - if (isset($_GET[$this->client->slug . '_tracker_optout']) && isset($_GET[$this->client->slug . '_tracker_optout']) && $_GET[$this->client->slug . '_tracker_optout'] === 'true') { - $this->optout(); + /** + * Check if the current page is updater.php or similar inaccessible pages. + * + * @return bool + */ + private function is_inaccessible_page() { + $inaccessible_pages = array( + '/wp-admin/update.php', // Add similar inaccessible PHP files here + ); + + // Sanitize and unslash the REQUEST_URI before using it + $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; + + // Ensure REQUEST_URI is properly sanitized before use + $request_uri = esc_url_raw( $request_uri ); - wp_safe_redirect(remove_query_arg($this->client->slug . '_tracker_optout')); - exit; + foreach ( $inaccessible_pages as $page ) { + if ( false !== strpos( $request_uri, $page ) ) { + return true; + } } + + return false; } /** @@ -520,19 +551,15 @@ public function handle_optin_optout() * * @return void */ - public function optin() - { - update_option($this->client->slug . '_allow_tracking', 'yes'); - update_option($this->client->slug . '_tracking_notice', 'hide'); + public function optin() { + update_option( $this->client->slug . '_allow_tracking', 'yes' ); + update_option( $this->client->slug . '_tracking_notice', 'hide' ); $this->clear_schedule_event(); $this->schedule_event(); $this->send_tracking_data(); - /* - * Fires when the user has opted in tracking. - */ - do_action($this->client->slug . '_tracker_optin', $this->get_tracking_data()); + do_action( $this->client->slug . '_tracker_optin', $this->get_tracking_data() ); } /** @@ -540,36 +567,31 @@ public function optin() * * @return void */ - public function optout() - { - update_option($this->client->slug . '_allow_tracking', 'no'); - update_option($this->client->slug . '_tracking_notice', 'hide'); + public function optout() { + update_option( $this->client->slug . '_allow_tracking', 'no' ); + update_option( $this->client->slug . '_tracking_notice', 'hide' ); $this->send_tracking_skipped_request(); $this->clear_schedule_event(); - /* - * Fires when the user has opted out tracking. - */ - do_action($this->client->slug . '_tracker_optout'); + do_action( $this->client->slug . '_tracker_optout' ); } /** * Get the number of post counts * - * @param string $post_type - * + * @param string $post_type The post type to count. * @return int */ - public function get_post_count($post_type) - { + public function get_post_count( $post_type ) { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( "SELECT count(ID) FROM $wpdb->posts WHERE post_type = %s and post_status = %s", - [$post_type, 'publish'] + $post_type, + 'publish' ) ); } @@ -579,28 +601,26 @@ public function get_post_count($post_type) * * @return array */ - private static function get_server_info() - { + private static function get_server_info() { global $wpdb; - $server_data = []; + $server_data = array(); - if (isset($_SERVER['SERVER_SOFTWARE']) && !empty($_SERVER['SERVER_SOFTWARE'])) { - // phpcs:ignore - $server_data['software'] = $_SERVER['SERVER_SOFTWARE']; + if ( isset( $_SERVER['SERVER_SOFTWARE'] ) && ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) { + $server_data['software'] = sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ); } - if (function_exists('phpversion')) { + if ( function_exists( 'phpversion' ) ) { $server_data['php_version'] = phpversion(); } $server_data['mysql_version'] = $wpdb->db_version(); - $server_data['php_max_upload_size'] = size_format(wp_max_upload_size()); + $server_data['php_max_upload_size'] = size_format( wp_max_upload_size() ); $server_data['php_default_timezone'] = date_default_timezone_get(); - $server_data['php_soap'] = class_exists('SoapClient') ? 'Yes' : 'No'; - $server_data['php_fsockopen'] = function_exists('fsockopen') ? 'Yes' : 'No'; - $server_data['php_curl'] = function_exists('curl_init') ? 'Yes' : 'No'; + $server_data['php_soap'] = class_exists( 'SoapClient' ) ? 'Yes' : 'No'; + $server_data['php_fsockopen'] = function_exists( 'fsockopen' ) ? 'Yes' : 'No'; + $server_data['php_curl'] = function_exists( 'curl_init' ) ? 'Yes' : 'No'; return $server_data; } @@ -610,23 +630,22 @@ private static function get_server_info() * * @return array */ - private function get_wp_info() - { - $wp_data = []; - - $wp_data['memory_limit'] = WP_MEMORY_LIMIT; - $wp_data['debug_mode'] = (defined('WP_DEBUG') && WP_DEBUG) ? 'Yes' : 'No'; - $wp_data['locale'] = get_locale(); - $wp_data['version'] = get_bloginfo('version'); - $wp_data['multisite'] = is_multisite() ? 'Yes' : 'No'; - $wp_data['theme_slug'] = get_stylesheet(); + private function get_wp_info() { + $wp_data = array( + 'memory_limit' => WP_MEMORY_LIMIT, + 'debug_mode' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'Yes' : 'No', + 'locale' => get_locale(), + 'version' => get_bloginfo( 'version' ), + 'multisite' => is_multisite() ? 'Yes' : 'No', + 'theme_slug' => get_stylesheet(), + ); - $theme = wp_get_theme($wp_data['theme_slug']); + $theme = wp_get_theme( $wp_data['theme_slug'] ); - $wp_data['theme_name'] = $theme->get('Name'); - $wp_data['theme_version'] = $theme->get('Version'); - $wp_data['theme_uri'] = $theme->get('ThemeURI'); - $wp_data['theme_author'] = $theme->get('Author'); + $wp_data['theme_name'] = $theme->get( 'Name' ); + $wp_data['theme_version'] = $theme->get( 'Version' ); + $wp_data['theme_uri'] = $theme->get( 'ThemeURI' ); + $wp_data['theme_author'] = $theme->get( 'Author' ); return $wp_data; } @@ -636,51 +655,42 @@ private function get_wp_info() * * @return array */ - private function get_all_plugins() - { - // Ensure get_plugins function is loaded - if (!function_exists('get_plugins')) { + private function get_all_plugins() { + if ( ! function_exists( 'get_plugins' ) ) { include ABSPATH . '/wp-admin/includes/plugin.php'; } $plugins = get_plugins(); - $active_plugins_keys = get_option('active_plugins', []); - $active_plugins = []; - - foreach ($plugins as $k => $v) { - // Take care of formatting the data how we want it. - $formatted = []; - $formatted['name'] = wp_strip_all_tags($v['Name']); - - if (isset($v['Version'])) { - $formatted['version'] = wp_strip_all_tags($v['Version']); - } - - if (isset($v['Author'])) { - $formatted['author'] = wp_strip_all_tags($v['Author']); + $active_plugins_keys = get_option( 'active_plugins', array() ); + $active_plugins = array(); + + foreach ( $plugins as $k => $v ) { + $formatted = array( + 'name' => wp_strip_all_tags( $v['Name'] ), + 'version' => wp_strip_all_tags( $v['Version'] ), + 'author' => wp_strip_all_tags( $v['Author'] ), + ); + + if ( isset( $v['Network'] ) ) { + $formatted['network'] = wp_strip_all_tags( $v['Network'] ); } - if (isset($v['Network'])) { - $formatted['network'] = wp_strip_all_tags($v['Network']); + if ( isset( $v['PluginURI'] ) ) { + $formatted['plugin_uri'] = wp_strip_all_tags( $v['PluginURI'] ); } - if (isset($v['PluginURI'])) { - $formatted['plugin_uri'] = wp_strip_all_tags($v['PluginURI']); - } - - if (in_array($k, $active_plugins_keys, true)) { - // Remove active plugins from list so we can show active and inactive separately - unset($plugins[$k]); - $active_plugins[$k] = $formatted; + if ( in_array( $k, $active_plugins_keys, true ) ) { + unset( $plugins[ $k ] ); + $active_plugins[ $k ] = $formatted; } else { - $plugins[$k] = $formatted; + $plugins[ $k ] = $formatted; } } - return [ - 'active_plugins' => $active_plugins, - 'inactive_plugins' => $plugins, - ]; + return array( + 'active_plugins' => $active_plugins, + 'inactive_plugins' => $plugins, + ); } /** @@ -688,19 +698,16 @@ private function get_all_plugins() * * @return array */ - public function get_user_counts() - { - $user_count = []; + public function get_user_counts() { + $user_count = array(); $user_count_data = count_users(); $user_count['total'] = $user_count_data['total_users']; - // Get user count based on user role - foreach ($user_count_data['avail_roles'] as $role => $count) { - if (!$count) { + foreach ( $user_count_data['avail_roles'] as $role => $count ) { + if ( ! $count ) { continue; } - - $user_count[$role] = $count; + $user_count[ $role ] = $count; } return $user_count; @@ -709,16 +716,14 @@ public function get_user_counts() /** * Add weekly cron schedule * - * @param array $schedules - * + * @param array $schedules Existing cron schedules. * @return array */ - public function add_weekly_schedule($schedules) - { - $schedules['weekly'] = [ + public function add_weekly_schedule( $schedules ) { + $schedules['weekly'] = array( 'interval' => DAY_IN_SECONDS * 7, - 'display' => 'Once Weekly', - ]; + 'display' => __( 'Once Weekly', 'appsero' ), + ); return $schedules; } @@ -728,25 +733,22 @@ public function add_weekly_schedule($schedules) * * @return void */ - public function activate_plugin() - { - $allowed = get_option($this->client->slug . '_allow_tracking', 'no'); + public function activate_plugin() { + $allowed = get_option( $this->client->slug . '_allow_tracking', 'no' ); - // if it wasn't allowed before, do nothing - if ('yes' !== $allowed) { + if ( 'yes' !== $allowed ) { return; } - // re-schedule and delete the last sent time so we could force send again $hook_name = $this->client->slug . '_tracker_send_event'; - if (!wp_next_scheduled($hook_name)) { - wp_schedule_event(time(), 'weekly', $hook_name); + if ( ! wp_next_scheduled( $hook_name ) ) { + wp_schedule_event( time(), 'weekly', $hook_name ); } - delete_option($this->client->slug . '_tracking_last_send'); + delete_option( $this->client->slug . '_tracking_last_send' ); - $this->send_tracking_data(true); + $this->send_tracking_data( true ); } /** @@ -754,16 +756,15 @@ public function activate_plugin() * * @return void */ - public function deactivation_cleanup() - { + public function deactivation_cleanup() { $this->clear_schedule_event(); - if ('theme' === $this->client->type) { - delete_option($this->client->slug . '_tracking_last_send'); - delete_option($this->client->slug . '_allow_tracking'); + if ( 'theme' === $this->client->type ) { + delete_option( $this->client->slug . '_tracking_last_send' ); + delete_option( $this->client->slug . '_allow_tracking' ); } - delete_option($this->client->slug . '_tracking_notice'); + delete_option( $this->client->slug . '_tracking_notice' ); } /** @@ -773,10 +774,9 @@ public function deactivation_cleanup() * * @return array */ - public function plugin_action_links($links) - { - if (array_key_exists('deactivate', $links)) { - $links['deactivate'] = str_replace(' 'could-not-understand', - 'text' => $this->client->__trans("Couldn't understand"), - 'placeholder' => $this->client->__trans('Would you like us to assist you?'), + 'text' => $this->client->__trans( "Couldn't understand" ), + 'placeholder' => $this->client->__trans( 'Would you like us to assist you?' ), 'icon' => '', ], [ 'id' => 'found-better-plugin', - 'text' => $this->client->__trans('Found a better plugin'), - 'placeholder' => $this->client->__trans('Which plugin?'), + 'text' => $this->client->__trans( 'Found a better plugin' ), + 'placeholder' => $this->client->__trans( 'Which plugin?' ), 'icon' => '', ], [ 'id' => 'not-have-that-feature', - 'text' => $this->client->__trans('Missing a specific feature'), - 'placeholder' => $this->client->__trans('Could you tell us more about that feature?'), + 'text' => $this->client->__trans( 'Missing a specific feature' ), + 'placeholder' => $this->client->__trans( 'Could you tell us more about that feature?' ), 'icon' => '', ], [ 'id' => 'is-not-working', - 'text' => $this->client->__trans('Not working'), - 'placeholder' => $this->client->__trans('Could you tell us a bit more whats not working?'), + 'text' => $this->client->__trans( 'Not working' ), + 'placeholder' => $this->client->__trans( 'Could you tell us a bit more whats not working?' ), 'icon' => '', ], [ 'id' => 'looking-for-other', - 'text' => $this->client->__trans('Not what I was looking'), - 'placeholder' => $this->client->__trans('Could you tell us a bit more?'), + 'text' => $this->client->__trans( 'Not what I was looking' ), + 'placeholder' => $this->client->__trans( 'Could you tell us a bit more?' ), 'icon' => '', ], [ 'id' => 'did-not-work-as-expected', - 'text' => $this->client->__trans("Didn't work as expected"), - 'placeholder' => $this->client->__trans('What did you expect?'), + 'text' => $this->client->__trans( "Didn't work as expected" ), + 'placeholder' => $this->client->__trans( 'What did you expect?' ), 'icon' => '', ], [ 'id' => 'other', - 'text' => $this->client->__trans('Others'), - 'placeholder' => $this->client->__trans('Could you tell us a bit more?'), + 'text' => $this->client->__trans( 'Others' ), + 'placeholder' => $this->client->__trans( 'Could you tell us a bit more?' ), 'icon' => '', ], ]; @@ -842,34 +841,33 @@ private function get_uninstall_reasons() * * @return void */ - public function uninstall_reason_submission() - { - if (!isset($_POST['nonce'])) { + public function uninstall_reason_submission() { + if ( ! isset( $_POST['nonce'] ) ) { return; } - if (!isset($_POST['reason_id'])) { + if ( ! isset( $_POST['reason_id'] ) ) { wp_send_json_error(); } - if (!wp_verify_nonce(sanitize_key(wp_unslash($_POST['nonce'])), 'appsero-security-nonce')) { - wp_send_json_error('Nonce verification failed'); + if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['nonce'] ) ), 'appsero-security-nonce' ) ) { + wp_send_json_error( 'Nonce verification failed' ); } - if (!current_user_can('manage_options')) { - wp_send_json_error('You are not allowed for this task'); + if ( ! current_user_can( 'manage_options' ) ) { + wp_send_json_error( 'You are not allowed for this task' ); } $data = $this->get_tracking_data(); - $data['reason_id'] = sanitize_text_field(wp_unslash($_POST['reason_id'])); - $data['reason_info'] = isset($_REQUEST['reason_info']) ? trim(sanitize_text_field(wp_unslash($_REQUEST['reason_info']))) : ''; + $data['reason_id'] = sanitize_text_field( wp_unslash( $_POST['reason_id'] ) ); + $data['reason_info'] = isset( $_REQUEST['reason_info'] ) ? trim( sanitize_text_field( wp_unslash( $_REQUEST['reason_info'] ) ) ) : ''; - $this->client->send_request($data, 'deactivate'); + $this->client->send_request( $data, 'deactivate' ); /* * Fire after the plugin _uninstall_reason_submitted */ - do_action($this->client->slug . '_uninstall_reason_submitted', $data); + do_action( $this->client->slug . '_uninstall_reason_submitted', $data ); wp_send_json_success(); } @@ -879,29 +877,28 @@ public function uninstall_reason_submission() * * @return void */ - public function deactivate_scripts() - { + public function deactivate_scripts() { global $pagenow; - if ('plugins.php' !== $pagenow) { + if ( 'plugins.php' !== $pagenow ) { return; } $this->deactivation_modal_styles(); $reasons = $this->get_uninstall_reasons(); - $custom_reasons = apply_filters('appsero_custom_deactivation_reasons', [], $this->client); -?> + $custom_reasons = apply_filters( 'appsero_custom_deactivation_reasons', [], $this->client ); + ?>
-

client->_etrans('Goodbyes are always hard. If you have a moment, please let us know how we can improve.'); ?>

+

client->_etrans( 'Goodbyes are always hard. If you have a moment, please let us know how we can improve.' ); ?>

    - -
  • + +
- +
    - -
  • + +
@@ -975,13 +972,13 @@ public function deactivate_scripts() } else { $('ul.wd-de-reasons li').removeClass('wd-de-reason-selected'); - if ("other" != inputValue) { + if ( "other" !== inputValue ) { $('ul.wd-de-reasons.wd-de-others-reasons').css('display', 'none'); } } // Show if has custom reasons - if ("other" == inputValue) { + if ( "other" === inputValue ) { $('ul.wd-de-reasons.wd-de-others-reasons').css('display', 'flex'); } @@ -1008,7 +1005,7 @@ public function deactivate_scripts() url: ajaxurl, type: 'POST', data: { - nonce: '', + nonce: '', action: 'client->slug; ?>_submit-uninstall-reason', reason_id: (0 === $radio.length) ? 'none' : $radio.val(), reason_info: (0 !== $input.length) ? $input.val().trim() : '' @@ -1026,7 +1023,7 @@ public function deactivate_scripts() }(jQuery)); - get_template() === $this->client->slug) { - $this->client->send_request($this->get_tracking_data(), 'deactivate'); + if ( $old_theme->get_template() === $this->client->slug ) { + $this->client->send_request( $this->get_tracking_data(), 'deactivate' ); } } /** * Get user IP Address */ - private function get_user_ip_address() - { - $response = wp_remote_get('https://icanhazip.com/'); + private function get_user_ip_address() { + $response = wp_remote_get( 'https://icanhazip.com/' ); - if (is_wp_error($response)) { + if ( is_wp_error( $response ) ) { return ''; } - $ip = trim(wp_remote_retrieve_body($response)); + $ip = trim( wp_remote_retrieve_body( $response ) ); - if (!filter_var($ip, FILTER_VALIDATE_IP)) { + if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { return ''; } @@ -1069,17 +1064,16 @@ private function get_user_ip_address() /** * Get site name */ - private function get_site_name() - { - $site_name = get_bloginfo('name'); + private function get_site_name() { + $site_name = get_bloginfo( 'name' ); - if (empty($site_name)) { - $site_name = get_bloginfo('description'); - $site_name = wp_trim_words($site_name, 3, ''); + if ( empty( $site_name ) ) { + $site_name = get_bloginfo( 'description' ); + $site_name = wp_trim_words( $site_name, 3, '' ); } - if (empty($site_name)) { - $site_name = esc_url(home_url()); + if ( empty( $site_name ) ) { + $site_name = esc_url( home_url() ); } return $site_name; @@ -1088,30 +1082,28 @@ private function get_site_name() /** * Send request to appsero if user skip to send tracking data */ - private function send_tracking_skipped_request() - { - $skipped = get_option($this->client->slug . '_tracking_skipped'); + private function send_tracking_skipped_request() { + $skipped = get_option( $this->client->slug . '_tracking_skipped' ); $data = [ 'hash' => $this->client->hash, 'previously_skipped' => false, ]; - if ($skipped === 'yes') { + if ( $skipped === 'yes' ) { $data['previously_skipped'] = true; } else { - update_option($this->client->slug . '_tracking_skipped', 'yes'); + update_option( $this->client->slug . '_tracking_skipped', 'yes' ); } - $this->client->send_request($data, 'tracking-skipped'); + $this->client->send_request( $data, 'tracking-skipped' ); } /** * Deactivation modal styles */ - private function deactivation_modal_styles() - { - ?> + private function deactivation_modal_styles() { + ?> -