/** * Gets an instance of the WP_DFP_Ad_Slot class * * @since 1.0 * * @param string|WP_Post $slot The name of the ad slot or a WP_Post object. * * @return WP_DFP_Ad_Slot */ function wp_dfp_ad_slot($slot) { if (!class_exists('WP_DFP_Ad_Slot')) { WP_DFP::inc('class-wp-dfp-ad-slot.php'); } try { return new WP_DFP_Ad_Slot($slot); } catch (InvalidArgumentException $e) { return new WP_Error(); } }
/** * Initialization functionality * * @since 1.0 */ public static function init() { $c = get_called_class(); WP_DFP::inc('externals/wp-forms-api/wp-forms-api.php'); add_filter('enter_title_here', array($c, 'change_enter_title_here_text')); add_filter('wp_form_base_url', array($c, 'wp_form_base_url')); add_action('save_post_' . WP_DFP::POST_TYPE, array($c, 'save_post')); add_filter('wp_insert_post_data', array($c, 'insert_post_data'), 10, 2); add_filter('manage_' . WP_DFP::POST_TYPE . '_posts_columns', array($c, 'manage_admin_columns')); add_action('manage_' . WP_DFP::POST_TYPE . '_posts_custom_column', array($c, 'admin_column_values'), 10, 2); add_action('admin_notices', array($c, 'display_admin_notices')); add_action('admin_action_' . self::CLONE_ACTION, array($c, 'clone_slot')); add_filter('post_row_actions', array($c, 'row_actions'), 10, 2); }
/** * Gets the full path to the ad slot * * @since 1.0.1 * * @return string */ public function path() { if (!$this->post instanceof WP_Post) { return ''; } WP_DFP::inc('wp-dfp-settings.php'); return ltrim(WP_DFP_Settings::get('network_code', '') . '/' . $this->slot(), '/'); }
/** * 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)); }