*/ class AdUnblocker_Public { const ASSET_BUNDLE_OWNER = 'frontend'; const FALLBACK_RUNTIME_NAME = 'daau_app'; /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $file_name; private $settings; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct($plugin_name, $version) { $this->plugin_name = $plugin_name; $this->version = $version; $this->settings = get_option($this->plugin_name . '-options', array()); if (!is_array($this->settings)) { $this->settings = array(); } // echo $this->settings[$this->plugin_name . '-file-name']; } /** * Register the stylesheets for the public-facing side of the site. * * @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. */ if ( array_key_exists($this->plugin_name . '-file-name', $this->settings) && array_key_exists($this->plugin_name . '-status', $this->settings) ) { $asset_bundle = $this->get_public_asset_bundle(); if ($this->settings[$this->plugin_name . '-status'] || $this->is_preview_request()) { wp_enqueue_style( $asset_bundle['handle'], $asset_bundle['css_url'], array(), $this->version, 'all' ); } } } public function display_ads($content) { return $content . ''; } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { /** * 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. */ $this->settings = $this->get_options_data(); $is_preview_request = $this->is_preview_request(); if (!$is_preview_request && isset($this->settings[$this->plugin_name . '-hide-loggedin']) && $this->settings[$this->plugin_name . '-hide-loggedin'] === '1' && is_user_logged_in()) { return; } /** * Since: 1.2.21 * * Disable on individual pages */ global $post; $adunblocker_page_status = false; if (!empty($post) && isset($post->ID)) { // The $post object exists and has a valid ID. $adunblocker_page_status = get_post_meta( $post->ID, '_adunblocker_page_status', true ); } if (!$is_preview_request && $adunblocker_page_status == 'y') { return; } if (! $this->is_current_page_targeted()) { return; } if ( array_key_exists($this->plugin_name . '-file-name', $this->settings) && array_key_exists($this->plugin_name . '-status', $this->settings) ) { $asset_bundle = $this->get_public_asset_bundle(); if ((string) $this->settings[$this->plugin_name . '-status'] === '1') { if (file_exists($asset_bundle['js_path'])) { $plugin_data = []; $stats_data = array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('track-event'), ); $recovery_data = null; if ($this->is_pro_enabled()) { $recovery_service = new AdUnblocker_Recovery($this->plugin_name); $request_uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '/'; $recovery_data = $recovery_service->get_runtime_payload($request_uri); } $exclude = ['adunblocker-file-name', 'adunblocker-version']; foreach ($this->settings as $key => $value) { if (in_array($key, $exclude)) { continue; } $plugin_data[str_replace('adunblocker-', '', $key)] = $value; // polylang if (function_exists('pll__') && 'content' == str_replace('adunblocker-', '', $key)) { $plugin_data[str_replace('adunblocker-', '', $key)] = pll__($value); } } $js = 'var ' . $asset_bundle['runtime_name'] . '={ui:' . wp_json_encode($plugin_data) . ',stats:' . wp_json_encode($stats_data) . ',recovery:' . wp_json_encode($recovery_data) . '};'; wp_print_inline_script_tag($js, array('data-cfasync' => 'false')); } wp_enqueue_script( $asset_bundle['handle'], $asset_bundle['js_url'], apply_filters('daau_public_script_dependencies', array()), $this->version, false ); } } } /** * Determines whether popup should be displayed on current page. * * @return bool */ private function is_current_page_targeted() { $content_key = $this->plugin_name . '-content'; $content = isset($this->settings[$content_key]) ? trim((string) $this->settings[$content_key]) : ''; if ($content === '') { return false; } if ($this->is_preview_request()) { return true; } $target_mode_key = $this->plugin_name . '-page-target-mode'; $target_ids_key = $this->plugin_name . '-target-page-ids'; $target_mode = isset($this->settings[$target_mode_key]) ? sanitize_key($this->settings[$target_mode_key]) : 'all'; if ('selected' !== $target_mode) { return true; } $target_page_ids = isset($this->settings[$target_ids_key]) ? $this->settings[$target_ids_key] : array(); if (!is_array($target_page_ids) || empty($target_page_ids)) { return false; } $target_page_ids = array_values(array_unique(array_filter(array_map('absint', $target_page_ids)))); if (empty($target_page_ids)) { return false; } $current_page_id = absint(get_queried_object_id()); if ($current_page_id <= 0) { return false; } return in_array($current_page_id, $target_page_ids, true); } /** * Returns the popup selected for an authenticated preview request. * * @return WP_Post|null */ private function get_preview_popup() { if (!is_user_logged_in() || !isset($_GET['daau_preview_popup'], $_GET['daau_preview_nonce'])) { return null; } $popup_id = absint(wp_unslash($_GET['daau_preview_popup'])); $nonce = sanitize_text_field(wp_unslash($_GET['daau_preview_nonce'])); if ( $popup_id <= 0 || !wp_verify_nonce($nonce, 'daau-preview-popup-' . $popup_id) || !current_user_can('edit_post', $popup_id) ) { return null; } $popup = get_post($popup_id); if (!($popup instanceof WP_Post) || 'daau_popup' !== $popup->post_type || 'trash' === $popup->post_status) { return null; } return $popup; } /** * Determines whether the current request is an authorized popup preview. * * @return bool */ private function is_preview_request() { return $this->get_preview_popup() instanceof WP_Post; } public function add_lock_css($classes) { return $classes; } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function ajax_request_callback() { $this->settings = $this->get_options_data(); $this->check_ajax_referer('get-plugin-data'); if (!empty($this->settings[$this->plugin_name . '-polylang'])) { if (function_exists('pll__')) { // Deprecated if (!empty($this->settings[$this->plugin_name . '-title'])) { $this->settings[$this->plugin_name . '-title'] = pll__($this->settings[$this->plugin_name . '-title']); } if (isset($this->settings[$this->plugin_name . '-content'])) { $this->settings[$this->plugin_name . '-content'] = pll__($this->settings[$this->plugin_name . '-content']); } } } wp_send_json($this->settings); end_ajax(); } /** * @param mixed $return Value to be returned as response. * * @return null */ function end_ajax($return = false) { $return = apply_filters('daau_before_response', $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)); } } public function get_options_data() { $settings = array(); $settings = get_option($this->plugin_name . '-options', null); if (!is_array($settings) || empty($settings)) { return array(); } $selected_popup_data = $this->get_selected_popup_data($settings); $content_key = $this->plugin_name . '-content'; $popup_id_key = $this->plugin_name . '-popup-id'; if ('' !== $selected_popup_data['content']) { $settings[$content_key] = $this->render_popup_content($selected_popup_data['content']); } else { $settings[$content_key] = ''; } $settings = $this->apply_selected_popup_settings($settings, $selected_popup_data['settings']); $settings[$popup_id_key] = isset($selected_popup_data['id']) ? absint($selected_popup_data['id']) : 0; $settings[$this->plugin_name . '-dialog-label'] = __('Ad blocker notice', 'adunblocker'); $settings[$this->plugin_name . '-close-label'] = __('Close ad blocker notice', 'adunblocker'); if ($this->is_preview_request()) { $settings[$this->plugin_name . '-preview-mode'] = '1'; $settings[$this->plugin_name . '-status'] = '1'; } // If no popup matches this request, skip runtime output on this page. if ('' === $selected_popup_data['content']) { $settings[$this->plugin_name . '-page-target-mode'] = 'selected'; $settings[$this->plugin_name . '-target-page-ids'] = array(); } return $settings; } /** * Gets popup content and runtime settings for the current page. * * @param array $settings Plugin options. * * @return array */ private function get_selected_popup_data($settings) { $empty_data = array( 'id' => 0, 'content' => '', 'settings' => array(), ); if (!is_array($settings)) { return $empty_data; } if (!did_action('init')) { return $empty_data; } $preview_popup = $this->get_preview_popup(); if ($preview_popup instanceof WP_Post) { $post_content = is_string($preview_popup->post_content) ? $preview_popup->post_content : ''; return array( 'id' => (int) $preview_popup->ID, 'content' => $post_content, 'settings' => $this->extract_popup_runtime_settings($post_content), ); } $popups = apply_filters('daau_public_popups', get_posts(array( 'post_type' => 'daau_popup', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => -1, ))); if (empty($popups) || !is_array($popups)) { return $empty_data; } $current_page_id = absint(get_queried_object_id()); $all_pages_popup = null; foreach ($popups as $popup) { $post_content = is_string($popup->post_content) ? $popup->post_content : ''; if (trim($post_content) === '') { continue; } $popup_settings = $this->extract_popup_runtime_settings($post_content); $page_target_mode = isset($popup_settings['page_target_mode']) ? $popup_settings['page_target_mode'] : 'all'; $target_page_ids = isset($popup_settings['target_page_ids']) && is_array($popup_settings['target_page_ids']) ? array_values(array_unique(array_filter(array_map('absint', $popup_settings['target_page_ids'])))) : array(); if ( 'selected' === $page_target_mode && $current_page_id > 0 && !empty($target_page_ids) && in_array($current_page_id, $target_page_ids, true) ) { return array( 'id' => (int) $popup->ID, 'content' => $post_content, 'settings' => $popup_settings, ); } if ('all' === $page_target_mode && null === $all_pages_popup) { $all_pages_popup = array( 'id' => (int) $popup->ID, 'content' => $post_content, 'settings' => $popup_settings, ); } } if (is_array($all_pages_popup)) { return $all_pages_popup; } return $empty_data; } /** * Extracts popup runtime settings from popup block attributes. * * @param string $post_content Popup post content. * * @return array */ private function extract_popup_runtime_settings($post_content) { if (!is_string($post_content) || trim($post_content) === '' || !function_exists('parse_blocks')) { return array(); } $popup_block = $this->find_popup_block(parse_blocks($post_content)); if ( !is_array($popup_block) || !isset($popup_block['attrs']) || !is_array($popup_block['attrs']) ) { return array(); } $attrs = $popup_block['attrs']; $settings = array( 'type' => 'strict', 'display_delay_seconds' => 5, 'temp_delay_seconds' => 5, 'temp_close_condition' => 'timer', 'scope' => 'page', 'page_target_mode' => 'all', 'target_page_ids' => array(), 'show_refresh_button' => true, 'refresh_button_label' => __('I have allowed ads - refresh', 'adunblocker'), ); if (isset($attrs['popupType'])) { $type = sanitize_key($attrs['popupType']); if (in_array($type, array('strict', 'temp', 'dismissible'), true)) { $settings['type'] = $type; } } if (isset($attrs['popupTempDelaySeconds'])) { $settings['display_delay_seconds'] = max(0, min(600, absint($attrs['popupTempDelaySeconds']))); } if (isset($attrs['popupTempTimerSeconds'])) { $settings['temp_delay_seconds'] = max(1, min(600, absint($attrs['popupTempTimerSeconds']))); } else { $settings['temp_delay_seconds'] = max(1, min(600, absint($settings['display_delay_seconds']))); } if (isset($attrs['popupTempCloseCondition'])) { $temp_close_condition = sanitize_key($attrs['popupTempCloseCondition']); if (in_array($temp_close_condition, array('timer', 'button', 'timer_or_button'), true)) { $settings['temp_close_condition'] = $temp_close_condition; } } if (isset($attrs['popupScope'])) { $scope = sanitize_key($attrs['popupScope']); if (in_array($scope, array('page', 'session'), true)) { $settings['scope'] = $scope; } } if (isset($attrs['pageTargetMode'])) { $page_target_mode = sanitize_key($attrs['pageTargetMode']); if (in_array($page_target_mode, array('all', 'selected'), true)) { $settings['page_target_mode'] = $page_target_mode; } } if (isset($attrs['targetPageIds']) && is_array($attrs['targetPageIds'])) { $target_page_ids = array_values(array_unique(array_filter(array_map('absint', $attrs['targetPageIds'])))); $settings['target_page_ids'] = $target_page_ids; } if (isset($attrs['showRefreshButton'])) { $settings['show_refresh_button'] = (bool) $attrs['showRefreshButton']; } if (isset($attrs['refreshButtonLabel'])) { $refresh_button_label = sanitize_text_field((string) $attrs['refreshButtonLabel']); if ('' !== $refresh_button_label) { $settings['refresh_button_label'] = $refresh_button_label; } } return $settings; } /** * Finds first popup block in parsed blocks tree. * * @param array $blocks Parsed block list. * * @return array|null */ private function find_popup_block($blocks) { if (!is_array($blocks)) { return null; } foreach ($blocks as $block) { if (!is_array($block)) { continue; } if (isset($block['blockName']) && 'daau/popup' === $block['blockName']) { return $block; } if (!empty($block['innerBlocks'])) { $inner_match = $this->find_popup_block($block['innerBlocks']); if (is_array($inner_match)) { return $inner_match; } } } return null; } /** * Merges popup template settings into runtime settings payload. * * @param array $settings Plugin settings. * @param array $popup_settings Popup settings. * * @return array */ private function apply_selected_popup_settings($settings, $popup_settings) { if (!is_array($settings) || !is_array($popup_settings)) { return $settings; } if (isset($popup_settings['type'])) { $settings[$this->plugin_name . '-type'] = $popup_settings['type']; } if (isset($popup_settings['display_delay_seconds'])) { $settings[$this->plugin_name . '-delay'] = (string) (max(0, min(600, absint($popup_settings['display_delay_seconds']))) * 1000); } if (isset($popup_settings['temp_delay_seconds'])) { $settings[$this->plugin_name . '-temp-delay'] = (string) (max(1, min(600, absint($popup_settings['temp_delay_seconds']))) * 1000); } if (isset($popup_settings['temp_close_condition'])) { $settings[$this->plugin_name . '-temp-close-condition'] = $popup_settings['temp_close_condition']; } if (isset($popup_settings['scope'])) { $settings[$this->plugin_name . '-scope'] = $popup_settings['scope']; } if (isset($popup_settings['page_target_mode'])) { $settings[$this->plugin_name . '-page-target-mode'] = $popup_settings['page_target_mode']; } if (array_key_exists('target_page_ids', $popup_settings)) { $settings[$this->plugin_name . '-target-page-ids'] = $popup_settings['target_page_ids']; } if (array_key_exists('show_refresh_button', $popup_settings)) { $settings[$this->plugin_name . '-show-refresh-button'] = $popup_settings['show_refresh_button'] ? '1' : '0'; } if (isset($popup_settings['refresh_button_label'])) { $settings[$this->plugin_name . '-refresh-button-label'] = $popup_settings['refresh_button_label']; } return $settings; } private function render_popup_content($content) { if (!is_string($content)) { return ''; } $content = trim($content); if ($content === '') { return ''; } if (function_exists('has_blocks') && has_blocks($content)) { return do_blocks($content); } return wpautop($content); } /** * Returns the absolute path to the root of the website. * * @return string */ function get_absolute_root_file_path() { static $absolute_path; if (!empty($absolute_path)) { return $absolute_path; } $absolute_path = rtrim(ABSPATH, '\\/'); $site_url = rtrim(site_url('', 'http'), '\\/'); $home_url = rtrim(home_url('', 'http'), '\\/'); if ($site_url != $home_url) { $difference = str_replace($home_url, '', $site_url); if (strpos($absolute_path, $difference) !== false) { $absolute_path = rtrim(substr($absolute_path, 0, -strlen($difference)), '\\/'); } } return $absolute_path; } /** * Get the domain for the current site. * * @return string */ function get_domain_current_site() { if (!is_multisite()) { return ''; } $current_site = get_current_site(); return $current_site->domain; } private function get_file_name_option_key() { return $this->plugin_name . '-file-name'; } private function get_file_name_from_settings($settings) { $key = $this->get_file_name_option_key(); if (!is_array($settings) || !isset($settings[$key])) { return ''; } return sanitize_file_name((string) $settings[$key]); } private function get_public_asset_bundle() { $bundle_key = $this->get_file_name_from_settings($this->settings); $css_file_name = $bundle_key . '.css'; $js_file_name = $bundle_key . '.js'; $is_generated = AdUnblocker_Asset_Cache::is_ready( $bundle_key, self::ASSET_BUNDLE_OWNER, array($css_file_name, $js_file_name) ); if ($is_generated) { return array( 'handle' => sanitize_key($bundle_key), 'runtime_name' => $bundle_key, 'css_path' => AdUnblocker_Asset_Cache::get_path($bundle_key, $css_file_name), 'css_url' => AdUnblocker_Asset_Cache::get_url($bundle_key, $css_file_name), 'js_path' => AdUnblocker_Asset_Cache::get_path($bundle_key, $js_file_name), 'js_url' => AdUnblocker_Asset_Cache::get_url($bundle_key, $js_file_name), 'generated' => true, ); } return array( 'handle' => $this->plugin_name, 'runtime_name' => self::FALLBACK_RUNTIME_NAME, 'css_path' => plugin_dir_path(__FILE__) . 'css/adunblocker-public.css', 'css_url' => plugin_dir_url(__FILE__) . 'css/adunblocker-public.css', 'js_path' => plugin_dir_path(__FILE__) . 'js/adunblocker-public.js', 'js_url' => plugin_dir_url(__FILE__) . 'js/adunblocker-public.js', 'generated' => false, ); } private function delete_assets_for_file_name($file_name) { $file_name = sanitize_file_name((string) $file_name); if ($file_name === '') { return; } AdUnblocker_Asset_Cache::delete_bundle($file_name, self::ASSET_BUNDLE_OWNER); AdUnblocker_Asset_Cache::delete_legacy_frontend_bundle($file_name); } public function rebuild_frontend_assets($rotate_file_name = true) { $option_name = $this->plugin_name . '-options'; $settings = get_option($option_name, array()); if (!is_array($settings)) { $settings = array(); } $this->settings = $settings; $current_file_name = $this->get_file_name_from_settings($this->settings); $file_name_key = $this->get_file_name_option_key(); $rolled_back = false; $reason = 'ok'; $target_file_name = $current_file_name; $changed = false; if ($rotate_file_name) { $new_file_name = $current_file_name; $attempts = 0; while ($new_file_name === $current_file_name && $attempts < 5) { $new_file_name = AdUnblocker::get_random_string(); $attempts++; } if ($new_file_name === $current_file_name) { $new_file_name = sanitize_file_name($current_file_name . AdUnblocker::get_random_string(1, 1)); } $target_file_name = $new_file_name; $this->settings[$file_name_key] = $target_file_name; update_option($option_name, $this->settings); } $this->regenerate_files(); $effective_file_name = $target_file_name; if ($effective_file_name === '') { $updated_settings = get_option($option_name, array()); if (is_array($updated_settings)) { $this->settings = $updated_settings; $effective_file_name = $this->get_file_name_from_settings($this->settings); } } if ($effective_file_name === '') { return array( 'success' => false, 'file_name' => '', 'previous_file_name' => $current_file_name, 'rolled_back' => false, 'reason' => 'missing_file_name', 'changed' => false, ); } $success = AdUnblocker_Asset_Cache::is_ready( $effective_file_name, self::ASSET_BUNDLE_OWNER, array($effective_file_name . '.css', $effective_file_name . '.js') ); if (!$success && $rotate_file_name) { $this->settings = $settings; update_option($option_name, $this->settings); $effective_file_name = $this->get_file_name_from_settings($this->settings); $rolled_back = true; $reason = 'generation_failed'; } if ($success && $rotate_file_name) { $expected_file_name = $effective_file_name; $updated_settings = get_option($option_name, array()); if (!is_array($updated_settings)) { $updated_settings = array(); } $updated_settings[$file_name_key] = $expected_file_name; $this->settings = $updated_settings; update_option($option_name, $this->settings); $persisted_settings = get_option($option_name, array()); $persisted_file_name = $this->get_file_name_from_settings($persisted_settings); if ($persisted_file_name === '') { $persisted_file_name = $current_file_name; } if ($persisted_file_name !== $expected_file_name) { $success = false; $rolled_back = true; $reason = 'option_sync_failed'; $effective_file_name = $persisted_file_name; $changed = false; } else { $effective_file_name = $expected_file_name; $changed = ($current_file_name !== '' && $current_file_name !== $effective_file_name); if ($changed) { $this->delete_assets_for_file_name($current_file_name); } else { $reason = 'key_not_changed'; } } } return array( 'success' => $success, 'file_name' => $effective_file_name, 'previous_file_name' => $current_file_name, 'rolled_back' => $rolled_back, 'reason' => $reason, 'changed' => $changed, ); } /** * Check if css and js has been generated for the frontend, if not attempt to create them * * @since 1.1.3 */ public function regenerate_files() { $option_name = $this->plugin_name . '-options'; $settings = get_option($option_name, array()); if (!is_array($settings)) { $settings = array(); } // Prefer in-memory file-name during explicit rebuild so stale option-cache reads // cannot force regeneration back to the previous asset key. $file_name_key = $this->get_file_name_option_key(); $memory_file_name = $this->get_file_name_from_settings($this->settings); if ($memory_file_name !== '') { $settings[$file_name_key] = $memory_file_name; } $this->settings = $settings; $file_name = $this->get_file_name_from_settings($this->settings); $file_name = $this->maybe_generate_new_file_name($file_name); if (empty($file_name)) { $file_name = AdUnblocker::get_random_string(); $this->settings[$this->get_file_name_option_key()] = $file_name; update_option($option_name, $this->settings); } $css_file = plugin_dir_path(__DIR__) . 'public/css/adunblocker-public.css'; $js_file = plugin_dir_path(__DIR__) . 'public/js/adunblocker-public.js'; $css_file_name = $file_name . '.css'; $js_file_name = $file_name . '.js'; if (AdUnblocker_Asset_Cache::is_ready( $file_name, self::ASSET_BUNDLE_OWNER, array($css_file_name, $js_file_name) )) { return; } $css_contents = AdUnblocker_Asset_Cache::read($css_file); $js_contents = AdUnblocker_Asset_Cache::read($js_file); if (! is_string($css_contents) || ! is_string($js_contents)) { return; } $css_contents = str_replace('da-adunblocker', $file_name, $css_contents); $replaceables = array( 'daau_app' => $file_name, 'da-adunblocker' => $file_name, '_fn_ibf' => AdUnblocker::get_random_string(3, 6), ); $js_contents = str_replace( array_keys($replaceables), array_values($replaceables), $js_contents ); $css_written = AdUnblocker_Asset_Cache::write( $file_name, self::ASSET_BUNDLE_OWNER, $css_file_name, $css_contents ); $js_written = AdUnblocker_Asset_Cache::write( $file_name, self::ASSET_BUNDLE_OWNER, $js_file_name, $js_contents ); if (! $css_written || ! $js_written) { AdUnblocker_Asset_Cache::delete_bundle($file_name, self::ASSET_BUNDLE_OWNER); return; } AdUnblocker_Asset_Cache::delete_legacy_frontend_bundle($file_name); } /** * Daily reset the name * * @since 1.2.17 */ public function maybe_generate_new_file_name($file_name = null) { $file_name = sanitize_file_name((string) $file_name); if (! AdUnblocker_Asset_Cache::is_safe_bundle_key($file_name)) { $file_name = AdUnblocker::get_random_string(); $this->settings[$this->get_file_name_option_key()] = $file_name; update_option($this->plugin_name . '-options', $this->settings); } return $file_name; } /** * Reset a file name daily * * @since 1.2.17 */ public function update_filename() { if (false === get_transient($this->plugin_name . '_update_filename_expiry')) { set_transient($this->plugin_name . '_update_filename_expiry', 'true', DAY_IN_SECONDS); $this->delete_old_files(); $this->maybe_generate_new_file_name(); } } /** * Check if running a current version. If new version detected, delete old files. * * @since 1.2.0 */ public function check_version() { $version_key = $this->plugin_name . '-version'; // Check if the version key exists in the settings array if (array_key_exists($version_key, $this->settings) && $this->settings[$version_key] != DAAU_PLUGIN_VERSION) { $this->delete_old_files(); $this->sync_settings(); $this->update_version(); } } /** * Update version * * @since 1.2.17 */ public function update_version() { $this->settings[$this->plugin_name . '-version'] = DAAU_PLUGIN_VERSION; update_option($this->plugin_name . '-options', $this->settings); } /** * When new version - delete old css and js. * * @since 1.2.1 */ public function delete_old_files() { $file_name = $this->get_file_name_from_settings($this->settings); $this->delete_assets_for_file_name($file_name); } /** * When new version introduces new settings. Init them with defaults. * * @since 1.2.1 */ public function sync_settings() { $defaults = AdUnblocker::get_defaults(); if (!empty($this->settings)) { foreach ($defaults as $key => $value) { if (!array_key_exists($key, $this->settings)) { $this->settings[$key] = $value; } } } else { $this->settings = $defaults; } update_option($this->plugin_name . '-options', $this->settings); } public function output_protect_noscript_wall() { do_action('daau_render_noscript_extension'); } private function is_pro_enabled() { if (defined('DAAU_PRO_ACTIVE') && DAAU_PRO_ACTIVE) { return true; } return (bool) apply_filters('daau_stats_is_pro', false); } }