* @package Udp_Agent */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * UDP Agent class. */ class Udp_Agent { /** * Name of this agents parent. * * @since 1.0.0 * @access private * @var string $agent_name Name of this agents parent. */ private $agent_name; /** * Engine URL. * * @since 1.0.0 * @access private * @var string $engine url URL to send wp rest api connection request. */ private $engine_url; /** * Agent's parent folder location * * @since 1.0.0 * @access private * @var string $agent_root_dir Path to this agents parent folder. */ private $agent_root_dir; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $ver Version of this agent. * @param string $agent_root_dir Path to this agents parent folder. * @param string $engine_url URL to Engine server. */ public function __construct( $ver, $agent_root_dir, $engine_url ) { $this->version = $ver; $this->engine_url = $engine_url; $this->agent_root_dir = $agent_root_dir; $this->hooks(); } // ---------------------------------------------- // Hooks. // ---------------------------------------------- /** * All hooks will be called from this method. * * @since 1.0.0 */ private function hooks() { add_action( 'init', array( $this, 'on_init' ) ); add_action( 'admin_init', array( $this, 'on_admin_init' ) ); // custom cron. add_action( 'init', array( $this, 'udp_schedule_cron' ) ); } /** * Action that needs to be run on "init" hook. * * @since 1.0.0 */ public function on_init() { // process user tracking actions. if ( isset( $_GET['udp-agent-allow-access'] ) ) { //phpcs:ignore $this->process_user_tracking_choice(); } } /** * Actions that needs to be run on "admin init" hook. * * @since 1.0.0 */ public function on_admin_init() { $this->show_user_tracking_admin_notice(); // register and save settings data. register_setting( 'general', 'udp_agent_allow_tracking', array( 'sanitize_callback' => array( $this, 'get_settings_field_val' ), ) ); // show ui in settings page. add_settings_field( 'udp_agent_allow_tracking', __( 'Allow Anonymous Tracking', 'udp-agent' ), array( $this, 'show_settings_ui' ), 'general', 'default', array( 'label_for' => 'udp_agent_allow_tracking', ) ); } /** * Change the value of checkbox ( in settings page ) from integer to "yes" or "no" * and store in database. * * @since 1.0.0 * @param string $data Data to modify. */ public function get_settings_field_val( $data ) { if ( '1' === $data ) { return 'yes'; } else { return 'no'; } } // ---------------------------------------------- // Settings page UI. // ---------------------------------------------- /** * Generate markups to show in settings page. * * @since 1.0.0 */ public function show_settings_ui() { echo '
'; echo "'; echo wp_kses_data( 'Become a super contributor by sharing your non-sensitive WordPress data. We guarantee no sensitive data is collected. What data do we collect?' ) . '
'; } // ---------------------------------------------- // Show admin notice, for collecting user data. // ---------------------------------------------- /** * Show admin notice to collect user data. * * @since 1.0.0 */ public function show_user_tracking_admin_notice() { $show_admin_notice = true; $users_choice = get_option( 'udp_agent_allow_tracking' ); if ( 'later' !== $users_choice && ! empty( $users_choice ) ) { // user has already clicked "yes" or "no" in admin notice. // do not show this notice. $show_admin_notice = false; } else { $tracking_msg_last_shown_at = intval( get_option( 'udp_agent_tracking_msg_last_shown_at' ) ); if ( $tracking_msg_last_shown_at > ( time() - ( DAY_IN_SECONDS * 3 ) ) ) { // do not show, // if last admin notice was shown less than 1 day ago. $show_admin_notice = false; } } if ( ! $show_admin_notice ) { return; } $content = '' . sprintf( /* translators: %s: agent name */ __( '%s is asking to allow tracking your non-sensitive WordPress data?', 'udp-agent' ), $this->find_agent_name( $this->agent_root_dir ) ) . '
'; $content .= sprintf( /* translators: %s: agent allow access link, %s: Allow */ __( '%2$s', 'udp-agent' ), add_query_arg( 'udp-agent-allow-access', 'yes' ), 'Allow' ); $content .= sprintf( /* translators: %s: agent allow access link, %s: Allow */ __( '%2$s', 'udp-agent' ), add_query_arg( 'udp-agent-allow-access', 'no' ), 'Do not show again' ); $content .= sprintf( /* translators: %s: agent allow access link, %s: Allow */ __( '%2$s', 'udp-agent' ), add_query_arg( 'udp-agent-allow-access', 'later' ), 'Later' ); $content .= '
'; $this->show_admin_notice( 'warning', $content ); } /** * User has decided to allow or not allow user tracking. * save this value in database. * * @since 1.0.0 */ private function process_user_tracking_choice() { $users_choice = isset( $_GET['udp-agent-allow-access'] ) ? sanitize_text_field( wp_unslash( $_GET['udp-agent-allow-access'] ) ) : ''; //phpcs:ignore if ( empty( $users_choice ) ) { return; } // add data into database. update_option( 'udp_agent_allow_tracking', $users_choice ); if ( 'yes' === $users_choice ) { $this->do_handshake(); } update_option( 'udp_agent_tracking_msg_last_shown_at', time() ); // redirect back. wp_safe_redirect( remove_query_arg( 'udp-agent-allow-access' ) ); exit; } /** * A little helper function to show admin notice. * * @since 1.0.0 * @param string $error_class Error Class/Type. * @param string $msg Message. */ private function show_admin_notice( $error_class, $msg ) { add_action( 'load-index.php', function () use ( $error_class, $msg ) { add_action( 'admin_notices', function() use ( $error_class, $msg ) { $class = 'is-dismissible notice notice-' . $error_class; printf( '