plugin_name = $plugin_name; $this->version = $version; $this->options = $this->get_options(); $this->admin_assets = AdUnblocker_Admin_Assets::instance($plugin_name, $version); } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in AdUnblocker_Loader as all of the hooks are defined * in that particular class. * * The AdUnblocker_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, $this->get_admin_asset_url('style', 'css/adunblocker-admin.css'), array(), $this->get_admin_asset_version('css/adunblocker-admin.css'), 'all' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function enqueue_scripts($hook_suffix) { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in AdUnblocker_Loader as all of the hooks are defined * in that particular class. * * The AdUnblocker_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ if ($hook_suffix == 'toplevel_page_adunblocker-general-settings-page') { $current_tab = $this->get_current_tab(); $admin_script = 'js/' . $this->plugin_name . '-admin.js'; wp_enqueue_script( $this->plugin_name, $this->get_admin_asset_url('base', $admin_script), array('jquery'), $this->get_admin_asset_version($admin_script), false ); if (in_array($current_tab, array('statistics_settings', 'analytics_settings'), true)) { wp_enqueue_script( 'adunblocker-chartjs', $this->get_admin_asset_url('chart', 'js/chart.umd.min.js'), array(), $this->get_admin_asset_version('js/chart.umd.min.js'), true ); $stats_script = 'js/' . $this->plugin_name . '-admin-stats.js'; wp_enqueue_script( $this->plugin_name . '-stats', $this->get_admin_asset_url('stats', $stats_script), array('jquery', 'adunblocker-chartjs'), $this->get_admin_asset_version($stats_script), true ); $this->add_admin_asset_probe($this->plugin_name . '-stats'); } } if ($hook_suffix == 'adunblocker_page_adunblocker-help') { $help_script = 'js/' . $this->plugin_name . '-admin-help.js'; wp_enqueue_script( $this->plugin_name, $this->get_admin_asset_url('help', $help_script), array('jquery'), $this->get_admin_asset_version($help_script), false ); } } private function get_admin_asset_version($relative_path) { $file_path = plugin_dir_path(__FILE__) . ltrim($relative_path, '/'); if (file_exists($file_path)) { return (string) filemtime($file_path); } return $this->version; } private function get_admin_asset_url($logical_name, $relative_path) { $relative_path = ltrim((string) $relative_path, '/'); $fallback_url = plugin_dir_url(__FILE__) . $relative_path; $source_path = plugin_dir_path(__FILE__) . $relative_path; return $this->admin_assets->get_asset_url($logical_name, $source_path, $fallback_url); } private function add_admin_asset_probe($handle) { $script = <<<'JS' (function () { var checkAssets = function () { var root = document.getElementById("daau-stats-root"); var notice = document.getElementById("wp-resource-load-warning"); if (!root || !notice) { return; } var cssReady = window.getComputedStyle(document.documentElement) .getPropertyValue("--daau-admin-assets-ready").trim() === "1"; var scriptsReady = window.daauAdminBaseReady === true && window.daauAdminStatsReady === true && typeof window.Chart !== "undefined"; var proReady = root.getAttribute("data-is-pro") !== "1" || (window.daauAdminRecoveryReady === true && window.daauAdminProStatsReady === true); if (!cssReady || !scriptsReady || !proReady) { notice.style.display = "block"; } }; if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", function () { window.setTimeout(checkAssets, 0); }); } else { window.setTimeout(checkAssets, 0); } })(); JS; wp_add_inline_script($handle, $script, 'after'); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function localize_script() { $nonces = apply_filters('daau_nonces', array( 'clear_log' => wp_create_nonce('clear-log'), 'get_log' => wp_create_nonce('get-log'), 'stats_overview' => wp_create_nonce('stats-overview'), 'clear_stats' => wp_create_nonce('clear-stats'), 'public_recovery' => wp_create_nonce('daau-recovery-public'), )); $data = apply_filters('daau_data', array( 'this_url' => esc_html(addslashes(home_url())), 'ajax_url' => admin_url('admin-ajax.php'), 'nonces' => $nonces, 'is_pro' => $this->is_pro_enabled(), 'stats' => array( 'is_pro' => (bool) apply_filters('daau_stats_is_pro', false), 'retention_days' => max(1, absint(apply_filters('daau_stats_retention_days', 7))), ), 'recovery_admin' => array( 'rest_url' => esc_url_raw(rest_url('adunblocker/v1/')), 'rest_nonce' => wp_create_nonce('wp_rest'), 'active_tab' => $this->get_current_tab(), ), )); // wp_localize_script( $handle, $name, $data ); if (wp_script_is($this->plugin_name, 'enqueued')) { wp_localize_script( $this->plugin_name, 'daau_app', $data ); } if (wp_script_is($this->plugin_name . '-stats', 'enqueued')) { wp_localize_script( $this->plugin_name . '-stats', 'daau_app', $data ); } if (wp_script_is($this->plugin_name . '-recovery-admin', 'enqueued')) { wp_localize_script( $this->plugin_name . '-recovery-admin', 'daau_app', $data ); } } /** * Adds a settings page link to a menu * * @link https://codex.wordpress.org/Administration_Menus * @since 1.0.0 * @return void */ public function add_menu() { // Top-level page // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); // Submenu Page // add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function); // Add the menu item and page $page_title = 'AdUnblocker Settings'; $menu_title = 'AdUnblocker'; $capability = 'manage_options'; $slug = $this->plugin_name . '-general-settings-page'; $callback = array($this, 'page_options'); $icon = 'dashicons-welcome-widgets-menus'; $position = 100; add_menu_page($page_title, $menu_title, $capability, $slug, $callback, $icon, $position); add_submenu_page( $slug, apply_filters('adunblocker-settings-page-title', esc_html__('AdUnblocker Settings', 'adunblocker')), apply_filters('adunblocker-settings-menu-title', esc_html__('Settings', 'adunblocker')), 'manage_options', $slug, array($this, 'page_options') ); add_submenu_page( $slug, apply_filters('adunblocker-settings-page-title', esc_html__('AdUnblocker Help', 'adunblocker')), apply_filters('adunblocker-settings-menu-title', esc_html__('Help', 'adunblocker')), 'manage_options', $this->plugin_name . '-help', array($this, 'page_help') ); if (!$this->is_pro_enabled()) { global $submenu; if (isset($submenu[$slug]) && is_array($submenu[$slug])) { $submenu[$slug][] = array( sprintf( '%s', esc_html__('Upgrade to Pro - Analytics', 'adunblocker') ), 'manage_options', $this->get_upgrade_url('admin-sidebar'), ); } } } // add_menu() /** * Adds a prominent Pro link to the Free plugin row. * * @param array $links Existing plugin action links. * @return array */ public function add_plugin_action_links($links) { if ($this->is_pro_enabled() || !is_array($links)) { return $links; } $upgrade_link = sprintf( '%3$s', esc_url($this->get_upgrade_url('plugins-page')), esc_attr__('Upgrade AdUnblocker to Pro', 'adunblocker'), esc_html__('Upgrade to Pro - Analytics', 'adunblocker') ); array_unshift($links, $upgrade_link); return $links; } /** * Returns the tracked sales URL for an upgrade placement. * * @param string $placement UTM content value. * @return string */ private function get_upgrade_url($placement) { return add_query_arg( array( 'utm_source' => 'plugin', 'utm_medium' => 'web', 'utm_content' => sanitize_key($placement), 'utm_campaign' => 'proplugin', ), 'https://digitalapps.com/wordpress-plugins/adunblocker/' ); } private function get_settings_page_url($tab = 'general_settings') { return add_query_arg( array( 'page' => $this->plugin_name . '-general-settings-page', 'tab' => $tab, ), admin_url('admin.php') ); } public function handle_rebuild_frontend_assets() { if (!current_user_can('manage_options')) { wp_die(esc_html__('Sorry, you are not allowed to access this page.', 'adunblocker')); } check_admin_referer($this->plugin_name . '-rebuild-assets', $this->plugin_name . '-rebuild-assets-nonce'); $redirect_url = $this->get_settings_page_url('general_settings'); $status = 'error'; $file_name = ''; $previous_file_name = ''; $rebuild_result = array(); $changed = false; try { $plugin_public = new AdUnblocker_Public($this->plugin_name, $this->version); $rebuild_result = $plugin_public->rebuild_frontend_assets(true); $file_name = isset($rebuild_result['file_name']) ? sanitize_file_name((string) $rebuild_result['file_name']) : ''; $previous_file_name = isset($rebuild_result['previous_file_name']) ? sanitize_file_name((string) $rebuild_result['previous_file_name']) : ''; $changed = !empty($rebuild_result['changed']); if (!empty($rebuild_result['success'])) { $status = $changed ? 'success' : 'unchanged'; } elseif (!empty($rebuild_result['rolled_back'])) { $status = 'unchanged'; } } catch (Exception $e) { $status = 'error'; } $args = array('daau_rebuild' => $status); if ($file_name !== '') { $args['daau_file'] = $file_name; } if ($previous_file_name !== '') { $args['daau_prev_file'] = $previous_file_name; } if (isset($rebuild_result['reason']) && is_string($rebuild_result['reason']) && $rebuild_result['reason'] !== '') { $args['daau_rebuild_reason'] = sanitize_key($rebuild_result['reason']); } wp_safe_redirect(add_query_arg($args, $redirect_url)); exit; } /** * Sets the class variable $options */ private function get_options() { $options = array(); $list = array(); $options['general_options'] = get_option($this->plugin_name . '-options'); foreach ($options as $option) { if (is_array($option)) { $list = array_merge($list, $option); } } return $list; } // get_options() /** * Creates the options page * * @since 1.0.0 * @return void */ public function page_options() { include(plugin_dir_path(__FILE__) . 'partials/adunblocker-admin-page-settings.php'); } // page_options() /** * Creates the help page * * @since 1.0.0 * @return void */ public function page_help() { include(plugin_dir_path(__FILE__) . 'partials/adunblocker-admin-page-help.php'); } // page_help() /** * Creates the options page * * @since 1.0.0 * @return void */ public function page_sidebar() { include(plugin_dir_path(__FILE__) . 'partials/adunblocker-admin-page-sidebar.php'); } // page_options() /** * #1 Registers settings sections with WordPress */ public function register_sections() { // add_settings_section( $id, $title, $callback, $menu_slug ); add_settings_section( $this->plugin_name . '-options', apply_filters('adunblockersection-title', ''), array($this, 'section_options'), $this->plugin_name . '-general-settings-page' ); } // register_sections() /** * #2 Registers settings fields with WordPress */ public function register_fields() { // add_settings_field( $id, $title, $callback, $menu_slug, $section, $args ); add_settings_field( $this->plugin_name . '-status', apply_filters('adunblockerlabel-status', esc_html__('Status:', 'adunblocker')), array($this, 'field_toggle'), $this->plugin_name . '-general-settings-page', $this->plugin_name . '-options', array( 'id' => $this->plugin_name . '-status', 'description' => 'Toggle to activate', 'value' => 0 ) ); add_settings_field( $this->plugin_name . '-scrollable-status', apply_filters('adunblockerlabel-status', esc_html__('Scrollable:', 'adunblocker')), array($this, 'field_toggle'), $this->plugin_name . '-general-settings-page', $this->plugin_name . '-options', array( 'id' => $this->plugin_name . '-scrollable-status', 'description' => 'On: Page is scrollabe when popup is shown. Off: Page is not scrollable when popup is shown.', 'value' => 1 ) ); /** * Since: 1.2.21 */ add_settings_field( $this->plugin_name . '-hide-loggedin', apply_filters('adunblockerlabel-status', esc_html__('Hide popup for logged-in users:', 'adunblocker')), array($this, 'field_toggle'), $this->plugin_name . '-general-settings-page', $this->plugin_name . '-options', array( 'id' => $this->plugin_name . '-hide-loggedin', 'description' => 'Hide popup for logged-in users. Default: Always show popup', 'value' => 0 ) ); if (function_exists('pll_register_string')) : add_settings_field( $this->plugin_name . '-polylang', apply_filters('adunblockerlabel-polylang', esc_html__('Polylang support:', 'adunblocker')), array($this, 'field_toggle'), $this->plugin_name . '-general-settings-page', $this->plugin_name . '-options', array( 'id' => $this->plugin_name . '-polylang', 'description' => 'Toggle to enable Polylang support. You will be able to edit "String Translations". Content under String Translations will be shown to the user.', 'value' => 0 ) ); endif; add_settings_field( $this->plugin_name . '-file-name', '', array($this, 'field_text'), $this->plugin_name . '-settings', $this->plugin_name . '-options', array( 'id' => $this->plugin_name . '-file-name', 'type' => 'hidden', 'class' => 'hidden' ) ); } // register_fields() /** * Registers plugin settings * * @since 1.0.0 * @return void */ public function register_settings() { // register_setting( $option_group, $option_name, $sanitize_callback ); register_setting( $this->plugin_name . '-general-settings-group', $this->plugin_name . '-options', array($this, 'validate_general_options') ); } // register_settings() /** * Creates a checkbox field * * @param array $args The arguments for the field * @return string The HTML field */ public function field_checkbox($args) { $defaults['class'] = ''; $defaults['description'] = ''; $defaults['label'] = ''; $defaults['name'] = $this->plugin_name . '-options[' . $args['id'] . ']'; $defaults['default_value'] = 'y'; $defaults = apply_filters('adunblocker-field-checkbox-options-defaults', $defaults); $atts = wp_parse_args($args, $defaults); if (! empty($this->options[$atts['id']])) { $atts['value'] = $this->options[$atts['id']]; } include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-field-checkbox.php'); } // field_checkbox() /** * Creates a text field * * @param array $args The arguments for the field * @return string The HTML field */ public function field_text($args) { $defaults['class'] = 'text widefat'; $defaults['description'] = ''; $defaults['label'] = ''; $defaults['name'] = $this->plugin_name . '-options[' . $args['id'] . ']'; $defaults['placeholder'] = ''; $defaults['type'] = 'text'; $defaults['value'] = ''; $defaults = apply_filters('adunblocker-field-text-options-defaults', $defaults); $atts = wp_parse_args($args, $defaults); if (! empty($this->options[$atts['id']])) { $atts['value'] = $this->options[$atts['id']]; } include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-field-text.php'); } // field_text() public function alpha_color_picker($args) { $defaults['class'] = ''; $defaults['description'] = ''; $defaults['label'] = ''; $defaults['name'] = $this->plugin_name . '-options[' . $args['id'] . ']'; $defaults['placeholder'] = ''; $defaults['type'] = 'text'; $defaults['value'] = ''; $defaults = apply_filters('adunblocker-alpha-color-picker-options-defaults', $defaults); $atts = wp_parse_args($args, $defaults); if (! empty($this->options[$atts['id']])) { $atts['value'] = $this->options[$atts['id']]; } include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-alpha-color-picker.php'); } /** * Creates a text field * * @param array $args The arguments for the field * @return string The HTML field */ public function color_picker($args) { $defaults['class'] = 'text widefat'; $defaults['description'] = ''; $defaults['label'] = ''; $defaults['name'] = $this->plugin_name . '-options[' . $args['id'] . ']'; $defaults['placeholder'] = ''; $defaults['type'] = 'text'; $defaults['value'] = ''; $defaults = apply_filters('adunblocker-color-picker-options-defaults', $defaults); $atts = wp_parse_args($args, $defaults); if (! empty($this->options[$atts['id']])) { $atts['value'] = $this->options[$atts['id']]; } include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-color-picker.php'); } // field_text() /** * Creates a select field * * Note: label is blank since its created in the Settings API * * @param array $args The arguments for the field * @return string The HTML field */ public function field_select($args) { $defaults['aria'] = ''; $defaults['blank'] = ''; $defaults['class'] = 'widefat'; $defaults['context'] = ''; $defaults['description'] = ''; $defaults['label'] = ''; $defaults['name'] = $this->plugin_name . '-options[' . $args['id'] . ']'; $defaults['selections'] = array(); $defaults['value'] = ''; $defaults = apply_filters('adunblocker-field-select-options-defaults', $defaults); $atts = wp_parse_args($args, $defaults); if (! empty($this->options[$atts['id']])) { $atts['value'] = $this->options[$atts['id']]; } if (empty($atts['aria']) && ! empty($atts['description'])) { $atts['aria'] = $atts['description']; } elseif (empty($atts['aria']) && ! empty($atts['label'])) { $atts['aria'] = $atts['label']; } include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-field-select.php'); } // field_select() /** * Creates an editor field * * NOTE: ID must only be lowercase letter, no spaces, dashes, or underscores. * * @param array $args The arguments for the field * @return string The HTML field */ public function field_editor($args) { $defaults['description'] = ''; $defaults['settings'] = array('textarea_name' => $this->plugin_name . '-options[' . $args['id'] . ']'); $defaults['value'] = ''; $defaults = apply_filters('adunblocker-field-editor-options-defaults', $defaults); $atts = wp_parse_args($args, $defaults); if (! empty($this->options[$atts['id']])) { $atts['value'] = $this->options[$atts['id']]; } include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-field-editor.php'); } // field_editor() /** * Creates a select field * * Note: label is blank since its created in the Settings API * * @param array $args The arguments for the field * @return string The HTML field */ public function field_toggle($args) { $args['id'] = isset($args['id']) ? $args['id'] : ''; $defaults['aria'] = ''; $defaults['blank'] = ''; $defaults['class'] = 'widefat'; $defaults['context'] = ''; $defaults['description'] = ''; $defaults['label'] = ''; $defaults['name'] = $this->plugin_name . '-options[' . $args['id'] . ']'; $defaults['selections'] = array(); $defaults['value'] = 0; $defaults = apply_filters('adunblocker-field-toggle-options-defaults', $defaults); $atts = wp_parse_args($args, $defaults); if (array_key_exists($atts['id'], $this->options) && $this->options[$atts['id']] !== '') { $atts['value'] = $this->options[$atts['id']]; } if (empty($atts['aria']) && ! empty($atts['description'])) { $atts['aria'] = $atts['description']; } elseif (empty($atts['aria']) && ! empty($atts['label'])) { $atts['aria'] = $atts['label']; } include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-field-toggle.php'); } /** * Creates a settings section * * @since 1.0.0 * @param array $params Array of parameters for the section * @return mixed The settings section */ public function section_options($params) { include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-section-options.php'); } // section_options() /** * Creates a settings section * * @since 1.0.0 * @param array $params Array of parameters for the section * @return mixed The settings section */ public function section_content($params) { include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-section-content.php'); } // section_content() /** * Creates a settings section * * @since 1.0.0 * @param array $params Array of parameters for the section * @return mixed The settings section */ public function section_style($params) { include(plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-admin-section-style.php'); } // section_content() private function sanitizer($type, $data) { if (empty($type)) { return; } // if ( empty( $data ) ) { return; } $return = ''; $sanitizer = new AdUnblocker_Sanitize($this->plugin_name); $sanitizer->set_data($data); $sanitizer->set_type($type); $return = $sanitizer->clean(); unset($sanitizer); return $return; } // sanitizer() /** * Returns an array of options names, fields types, and default values * * @return array An array of options */ public function get_general_options_list() { $options = array(); // Adding standard options $options[] = array($this->plugin_name . '-status', 'checkbox'); $options[] = array($this->plugin_name . '-scrollable-status', 'checkbox'); $options[] = array($this->plugin_name . '-hide-loggedin', 'checkbox'); $options[] = array($this->plugin_name . '-polylang', 'checkbox'); // Deprecate - Check if the title option is set before accessing it if (isset($this->options) && isset($this->options[$this->plugin_name . '-title'])) { $options[] = array($this->plugin_name . '-title', 'text'); $options[] = array($this->plugin_name . '-title-color', 'color'); } // Adding additional options $options[] = array($this->plugin_name . '-content', 'blocks'); $options[] = array($this->plugin_name . '-overlay-color', 'text'); $options[] = array($this->plugin_name . '-bg-color', 'color'); $options[] = array($this->plugin_name . '-text-color', 'color'); $options[] = array($this->plugin_name . '-file-name', 'file'); return $options; } public function validate_general_options($input) { $db_options = $this->get_options(); $options = $this->get_general_options_list(); return $this->validate_options($input, $options, $db_options); } /** * Validates saved options * * @since 1.0.0 * @param array $input array of submitted plugin options * @return array array of validated plugin options */ public function validate_options($input, $options, $db_options) { if (null == $input) { add_settings_error( 'requiredTextFieldEmpty', 'empty', __('Cannot be empty', 'adunblocker'), 'error' ); } $valid = array(); // Loop through each option foreach ($options as $option) { // Check if each element in the $option array exists, and set default values if not $name = isset($option[0]) ? $option[0] : null; $type = isset($option[1]) ? $option[1] : 'text'; // Default to 'text' if $option[1] is not set $value = isset($option[2]) ? $option[2] : ''; // Default to empty string if $option[2] is not set if ($name !== null) { // Proceed only if a name is defined // Check if the name exists in the input array; otherwise, fall back to db_options if (array_key_exists($name, $input)) { $valid[$name] = $this->sanitizer($type, $input[$name]); } elseif (array_key_exists($name, $db_options)) { $valid[$name] = $db_options[$name]; } else { // If not in input or db_options, use the default value $valid[$name] = $value; } } } return $valid; } // validate_options() /** * @param mixed $return Value to be returned as response. * * @return null */ function end_ajax($return = false) { $return = apply_filters('daau_before_response', $return); if (defined('DOING_daau_TESTS')) { // This function should signal the end of the PHP process, but for CLI it carries on so we need to reset our own usage // of the daau_before_response filter before another respond_to_* function adds it again. remove_filter('daau_before_response', array($this, 'scramble')); return (false === $return) ? null : $return; } echo (false === $return) ? '' : wp_kses_post((string) $return); exit; } function check_ajax_referer($action) { $result = check_ajax_referer($action, 'nonce', false); if (false === $result) { /* translators: %s: AJAX action name. */ $return = array('daau_error' => 1, 'body' => sprintf(__('Invalid nonce for: %s', 'adunblocker'), $action)); $this->end_ajax(wp_json_encode($return)); } $cap = (is_multisite()) ? 'manage_network_options' : 'export'; $cap = apply_filters('daau_ajax_cap', $cap); if (! current_user_can($cap)) { /* translators: %s: AJAX action name. */ $return = array('daau_error' => 1, 'body' => sprintf(__('Access denied for: %s', 'adunblocker'), $action)); $this->end_ajax(wp_json_encode($return)); } } /** * Loads the error log into the error log class property. */ function load_error_log() { if (! is_null($this->error_log)) { return; } $this->error_log = get_site_option('daau_error_log'); /* * The error log was previously stored and retrieved using get_option and update_option respectively. * Here we update the subsite option to a network wide option if applicable. */ if (false === $this->error_log && is_multisite() && is_network_admin()) { $this->error_log = get_option($this->plugin_name . '_error_log'); if (false !== $this->error_log) { update_site_option($this->plugin_name . '_error_log', $this->error_log); delete_option($this->plugin_name . '_error_log'); } } } function ajax_get_log() { $this->check_ajax_referer('get-log'); ob_start(); $this->output_diagnostic_info(); // $this->output_log_file(); $return = ob_get_clean(); $result = $this->end_ajax($return); } function output_log_file() { $this->load_error_log(); if (isset($this->error_log)) { echo esc_html($this->error_log); } } /** * Outputs diagnostic info for debugging. * * Outputs useful diagnostic info text at the Diagnostic Info & Error Log * section under the Help tab so the information can be viewed or * downloaded and shared for debugging. * * If you would like to add additional diagnostic information use the * `daau_diagnostic_info` action hook (see {@link https://developer.wordpress.org/reference/functions/add_action/}). * * * add_action( 'daau_diagnostic_info', 'my_diagnostic_info' ) { * echo "Additional Diagnostic Info: \r\n"; * echo "...\r\n"; * } * * * @return void */ function output_diagnostic_info() { global $wpdb; $table_prefix = $wpdb->base_prefix; echo 'site_url(): '; echo esc_html(site_url()); echo "\r\n"; echo 'home_url(): '; echo esc_html(home_url()); echo "\r\n"; echo 'Database Name: '; echo esc_html($wpdb->dbname); echo "\r\n"; echo 'Table Prefix: '; echo esc_html($table_prefix); echo "\r\n"; echo 'WordPress: '; echo esc_html(get_bloginfo('version')); if (is_multisite()) { $multisite_type = defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL ? 'Sub-domain' : 'Sub-directory'; echo ' Multisite (' . esc_html($multisite_type) . ')'; echo "\r\n"; if (defined('DOMAIN_CURRENT_SITE')) { echo 'Domain Current Site: '; echo esc_html(DOMAIN_CURRENT_SITE); echo "\r\n"; } if (defined('PATH_CURRENT_SITE')) { echo 'Path Current Site: '; echo esc_html(PATH_CURRENT_SITE); echo "\r\n"; } if (defined('SITE_ID_CURRENT_SITE')) { echo 'Site ID Current Site: '; echo esc_html(SITE_ID_CURRENT_SITE); echo "\r\n"; } if (defined('BLOG_ID_CURRENT_SITE')) { echo 'Blog ID Current Site: '; echo esc_html(BLOG_ID_CURRENT_SITE); } } echo "\r\n"; echo 'Web Server: '; $server_software = isset($_SERVER['SERVER_SOFTWARE']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])) : ''; echo esc_html($server_software); echo "\r\n"; echo 'PHP: '; if (function_exists('phpversion')) { echo esc_html(phpversion()); } echo "\r\n"; echo 'MySQL: '; echo esc_html($wpdb->db_version()); echo "\r\n"; echo 'ext/mysqli: '; echo empty($wpdb->use_mysqli) ? 'no' : 'yes'; echo "\r\n"; echo 'WP Memory Limit: '; echo esc_html(WP_MEMORY_LIMIT); echo "\r\n"; echo 'Blocked External HTTP Requests: '; if (! defined('WP_HTTP_BLOCK_EXTERNAL') || ! WP_HTTP_BLOCK_EXTERNAL) { echo 'None'; } else { $accessible_hosts = (defined('WP_ACCESSIBLE_HOSTS')) ? WP_ACCESSIBLE_HOSTS : ''; if (empty($accessible_hosts)) { echo 'ALL'; } else { echo 'Partially (Accessible Hosts: ' . esc_html($accessible_hosts) . ')'; } } echo "\r\n"; echo 'WP Locale: '; echo esc_html(get_locale()); echo "\r\n"; echo 'DB Charset: '; echo esc_html(DB_CHARSET); echo "\r\n"; if (function_exists('ini_get') && $suhosin_limit = ini_get('suhosin.post.max_value_length')) { echo 'Suhosin Post Max Value Length: '; echo esc_html(is_numeric($suhosin_limit) ? size_format($suhosin_limit) : $suhosin_limit); echo "\r\n"; } if (function_exists('ini_get') && $suhosin_limit = ini_get('suhosin.request.max_value_length')) { echo 'Suhosin Request Max Value Length: '; echo esc_html(is_numeric($suhosin_limit) ? size_format($suhosin_limit) : $suhosin_limit); echo "\r\n"; } echo 'Debug Mode: '; echo esc_html((defined('WP_DEBUG') && WP_DEBUG) ? 'Yes' : 'No'); echo "\r\n"; echo 'WP Max Upload Size: '; echo esc_html(size_format(wp_max_upload_size())); echo "\r\n"; echo 'PHP Post Max Size: '; echo esc_html(size_format($this->get_post_max_size())); echo "\r\n"; echo 'PHP Time Limit: '; if (function_exists('ini_get')) { echo esc_html(ini_get('max_execution_time')); } echo "\r\n"; echo 'PHP Error Log: '; if (function_exists('ini_get')) { echo esc_html(ini_get('error_log')); } echo "\r\n"; echo 'fsockopen: '; if (function_exists('fsockopen')) { echo 'Enabled'; } else { echo 'Disabled'; } echo "\r\n"; echo 'OpenSSL: '; if ($this->open_ssl_enabled()) { echo esc_html(OPENSSL_VERSION_TEXT); } else { echo 'Disabled'; } echo "\r\n"; echo 'cURL: '; if (function_exists('curl_init')) { echo 'Enabled'; } else { echo 'Disabled'; } echo "\r\n"; do_action('daau_diagnostic_info'); if (has_action('daau_diagnostic_info')) { echo "\r\n"; } $theme_info = wp_get_theme(); echo "Active Theme Name: " . esc_html($theme_info->Name) . "\r\n"; echo "Active Theme Folder: " . esc_html(basename($theme_info->get_stylesheet_directory())) . "\r\n"; if ($theme_info->get('Template')) { echo "Parent Theme Folder: " . esc_html($theme_info->get('Template')) . "\r\n"; } if (! file_exists($theme_info->get_stylesheet_directory())) { echo "WARNING: Active Theme Folder Not Found\r\n"; } echo "\r\n"; echo "Active Plugins:\r\n"; $active_plugins = (array) get_option('active_plugins', array()); if (is_multisite()) { $network_active_plugins = wp_get_active_network_plugins(); $active_plugins = array_map(array($this, 'remove_wp_plugin_dir'), $network_active_plugins); } foreach ($active_plugins as $plugin) { $suffix = (isset($blacklist[$plugin])) ? '*' : ''; $this->print_plugin_details(WP_PLUGIN_DIR . '/' . $plugin, $suffix); } $mu_plugins = wp_get_mu_plugins(); if ($mu_plugins) { echo "\r\n"; echo "Must-use Plugins:\r\n"; foreach ($mu_plugins as $mu_plugin) { $this->print_plugin_details($mu_plugin); } echo "\r\n"; } } function open_ssl_enabled() { if (defined('OPENSSL_VERSION_TEXT')) { return true; } else { return false; } } /** * Returns the php ini value for post_max_size in bytes * * @return int */ function get_post_max_size() { $bytes = max(wp_convert_hr_to_bytes(trim(ini_get('post_max_size'))), wp_convert_hr_to_bytes(trim(ini_get('hhvm.server.max_post_size')))); return $bytes; } function print_plugin_details($plugin_path, $suffix = '') { $plugin_data = get_plugin_data($plugin_path); if (empty($plugin_data['Name'])) { return; } printf( "%s%s (v%s) by %s\r\n", esc_html($plugin_data['Name']), esc_html($suffix), esc_html($plugin_data['Version']), esc_html($plugin_data['AuthorName']) ); } /** * Check for daabd-download-log and related nonce * if found begin diagnostic logging * * @return void */ function http_prepare_download_log() { $download_requested = isset($_GET['daabd-download-log']); $download_nonce = isset($_GET['nonce']) ? sanitize_text_field(wp_unslash($_GET['nonce'])) : ''; if ($download_requested && wp_verify_nonce($download_nonce, 'daabd-download-log')) { ob_start(); $this->output_diagnostic_info(); $this->output_log_file(); $log = ob_get_clean(); $url = $this->parse_url(home_url()); $host = sanitize_file_name($url['host']); $filename = sprintf('%s-diagnostic-log-%s.txt', $host, gmdate('YmdHis')); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Length: ' . strlen($log)); header('Content-Disposition: attachment; filename=' . $filename); echo esc_html($log); exit; } } /** * Parses a url into its components. Compatible with PHP < 5.4.7. * * @param $url string The url to parse. * * @return array|false The parsed components or false on error. */ function parse_url($url) { $url = trim($url); if (0 === strpos($url, '//')) { $url = 'http:' . $url; $no_scheme = true; } else { $no_scheme = false; } $parts = wp_parse_url($url); if ($no_scheme) { unset($parts['scheme']); } return $parts; } /** * Displays admin notices * * @return string Admin notices */ public function display_admin_notices() { settings_errors(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin navigation parameter. $page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : ''; if ( $this->plugin_name . '-general-settings-page' !== $page || 'statistics_settings' !== $this->get_current_tab() ) { return; } ?> get_admin_tabs()); if (! in_array($active_tab, $allowed_tabs, true)) { $active_tab = 'statistics_settings'; } if (! $this->is_pro_enabled() && $this->is_recovery_tab($active_tab)) { return 'general_settings'; } return $active_tab; } /** * Returns tabs available in the AdUnblocker admin application. * Extensions can add their own views without creating disconnected admin pages. * * @return array */ public function get_admin_tabs() { $tabs = array( 'statistics_settings' => esc_html__('Analytics', 'adunblocker'), 'general_settings' => esc_html__('General', 'adunblocker'), 'journeys_settings' => esc_html__('Journeys', 'adunblocker'), 'experiments_settings' => esc_html__('Experiments', 'adunblocker'), 'offers_settings' => esc_html__('Offers', 'adunblocker'), ); return apply_filters('daau_admin_tabs', $tabs); } private function is_recovery_tab($tab) { return in_array( $tab, array('journeys_settings', 'experiments_settings', 'offers_settings'), true ); } private function is_pro_enabled() { if (defined('DAAU_PRO_ACTIVE') && DAAU_PRO_ACTIVE) { return true; } return (bool) apply_filters('daau_stats_is_pro', false); } /** * Polylang */ function content_pll_register_string() { if (function_exists('pll_register_string')) : $option = get_option($this->plugin_name . '-options'); // Deprecate if (! empty($this->options[$this->plugin_name . '-title'])) { $title = $option[$this->plugin_name . '-title']; } $content = isset($option[$this->plugin_name . '-content']) ? $option[$this->plugin_name . '-content'] : ''; // Deprecate if (! empty($this->options[$this->plugin_name . '-title'])) { pll_register_string('AdUnblocker Title', $title, 'adunblocker', true); } pll_register_string('AdUnblocker Content', $content, 'adunblocker', true); endif; } }