/** * Generates the targeting for the current content being displayed * * @since 1.0 * * @return array */ public static function get_targeting() { if (is_array(self::$targeting)) { return; } // Init targeting array self::$targeting = array(); // Include WP_DFP_Settings class WP_DFP::inc('wp-dfp-settings.php'); // Helper function for converting all array values to strings $to_string = create_function('$v', 'return is_array( $v ) ? $v : (string) $v;'); // Define targeting rules for home/front page if (is_front_page() || is_home()) { self::$targeting['post_name'] = 'home'; self::$targeting['post_title'] = 'Home'; self::$targeting['post_id'] = 0; self::$targeting['post_type'] = ''; } elseif (is_singular()) { global $post; $tag_terms = get_the_terms($post->ID, 'post_tag'); if (is_array($tag_terms)) { self::$targeting['tag'] = wp_list_pluck($tag_terms, 'slug'); } $category_terms = get_the_terms($post->ID, 'category'); if (is_array($category_terms)) { self::$targeting['category'] = array_map($to_string, wp_list_pluck($category_terms, 'slug')); } self::$targeting['post_name'] = $post->post_name; self::$targeting['post_title'] = $post->post_title; self::$targeting['post_id'] = $post->ID; self::$targeting['post_type'] = $post->post_type; } elseif (is_category()) { self::$targeting['taxonomy'] = 'category'; self::$targeting['term'] = get_queried_object()->slug; } elseif (is_tag()) { self::$targeting['taxonomy'] = 'tag'; self::$targeting['term'] = get_queried_object()->slug; } elseif (is_tax()) { self::$taxonomy = get_queried_object()->slug; self::$targeting['taxonomy'] = $taxonomy; self::$targeting['term'] = get_query_var($taxonomy); } elseif (is_search()) { self::$targeting['search_term'] = get_query_var('s'); } /** * Filter the targeting arguments for the current content being displayed * * @since 1.0 * * @param array $targeting An array of targeting arguments. */ self::$targeting = apply_filters('wp_dfp_targeting', self::$targeting); // Define slots $all_slots = WP_DFP::get_slots(); $slots = array(); foreach ($all_slots as $slot) { $slot = wp_dfp_ad_slot($slot); $slots[$slot->slot()] = $slot->sizes(); unset($slot); // prevent possible memory spike } // Get network code $network_code = WP_DFP_Settings::get('network_code'); // Set message defaults $messages = array('noNetworkCode' => null); /* If current user can manage options and a network code is not defined then display an message in each in-page ad slot */ if (empty($network_code) && current_user_can('manage_options')) { $messages['noNetworkCode'] = '<div class="wp-dfp-ad-unit-error">' . sprintf(__('WP_DFP: You must supply your DFP network code in the <a target="_blank" href="%s">WP DFP settings</a> screen.', 'wp-dfp'), wp_dfp_settings_url()) . '</div>'; } wp_enqueue_script('wp-dfp'); wp_localize_script('wp-dfp', 'wpdfp', array('network' => $network_code, 'targeting' => array_map($to_string, self::$targeting), 'slots' => $slots, 'messages' => $messages)); }
/** * Saves the plugin settings * * @since 1.0 * @action admin_init */ public static function save_settings() { if (!isset($_POST[self::NONCE_NAME])) { return; } check_admin_referer(self::NONCE_ACTION, self::NONCE_NAME); update_option(self::OPTION_NAME, array_map('trim', $_POST[self::OPTION_NAME])); // Redirect per best practices $url = add_query_arg(self::SETTINGS_SAVED_ARG, 1, wp_dfp_settings_url()); wp_redirect($url); exit; }
/** * Displays any pertinent admin notices * * @since 1.0 * @action admin_notices */ public static function display_admin_notices() { $network_code = WP_DFP_Settings::get('network_code'); if (!empty($network_code) || get_current_screen()->id == 'wp_dfp_ad_slot_page_wp_dfp_settings') { return; } if (current_user_can('manage_options')) { $notice = sprintf(__('WP_DFP: You have not specified your DFP network code. You can set your network code by going to the <a href="%s">WP DFP settings page</a>', 'wp-dfp'), wp_dfp_settings_url()); } else { $notice = __('WP_DFP: A DFP network code is required for WP DFP to function correctly. Please have an admin set a network code ASAP.', 'wp-dfp'); } echo '<div class="error"><p>' . $notice . '</p></div>'; }