/**
  * Setup the singleton.
  *
  * @access public
  */
 public function setup()
 {
     // Register the ad server settings page
     add_action('init', array($this, 'add_settings_page'));
     // Add the required header and footer setup. May differ for each server.
     add_action('wp_head', array($this, 'header_setup'));
     add_action('wp_footer', array($this, 'footer_setup'));
     // Handle rendering units
     add_action('ad_layers_render_ad_unit', array($this, 'get_ad_unit'));
     // Load the Javascript API early
     add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'), 5);
     // Load current settings
     self::$settings = apply_filters('ad_layers_ad_server_settings', get_option($this->option_name, array()));
     // Allow additional ad servers to be loaded via filter within a theme
     $this->ad_servers = apply_filters('ad_layers_ad_servers', array('Ad_Layers_DFP' => AD_LAYERS_BASE_DIR . '/php/ad-servers/class-ad-layers-dfp.php'));
     // Load ad server classes
     if (!empty($this->ad_servers) && is_array($this->ad_servers)) {
         foreach ($this->ad_servers as $ad_server) {
             if (file_exists($ad_server)) {
                 require_once $ad_server;
             }
         }
     }
     // Set the current ad server class, if defined.
     if (!empty(self::$settings['ad_server']) && class_exists(self::$settings['ad_server'])) {
         $ad_server = new self::$settings['ad_server']();
         $this->ad_server = $ad_server::instance();
     }
 }
 /**
  * Add an ad unit to the body of a post
  *
  * @access public
  * @param array $atts
  * @param string $content
  * @param string $tag
  * @return string
  */
 public function do_ad_unit($atts, $content, $tag)
 {
     // Ensure the shortcode is valid
     if (empty($atts['unit'])) {
         return;
     }
     // Attempt to display the specified ad unit.
     // This will just do nothing if the unit is invalid
     // or doesn't exist in the current ad layer.
     return Ad_Layers_Ad_Server::instance()->get_ad_unit($atts['unit']);
 }
 public function setUp()
 {
     parent::setUp();
     $this->ad_server_parent = Ad_Layers_Ad_Server::instance();
     $this->ad_server_settings = array('ad_server' => 'Ad_Layers_DFP', 'account_id' => '6355419', 'path_templates' => array(array('path_template' => '/#account_id#/#ad_unit#/front', 'page_type' => 'default')), 'breakpoints' => array(), 'ad_units' => array(array('code' => 'sidebar', 'sizes' => array(array('width' => 300, 'height' => 250, 'default_size' => 'default'), array('width' => 300, 'height' => 600)))));
     update_option($this->ad_server_parent->option_name, $this->ad_server_settings);
     // Add an ad layer
     $this->ad_layer = $this->factory->post->create(array('post_type' => 'ad-layer'));
     add_post_meta($this->ad_layer, 'ad_layer_ad_units', array(array('ad_unit' => 'sidebar')));
     // add_post_meta( $this->ad_layer, 'ad_layer_page_types', array() );
     Ad_Layers::instance()->setup();
     $this->ad_server_parent->setup();
     $this->ad_server = $this->ad_server_parent->get_ad_server();
 }
 /**
  * Add an ad unit to the body of a post
  *
  * @access public
  * @param array $atts
  * @param string $content
  * @param string $tag
  * @return string
  */
 public function do_ad_unit($atts, $content, $tag)
 {
     // Ensure the shortcode is valid
     if (empty($atts['unit'])) {
         return;
     }
     // Attempt to display the specified ad unit.
     // This will just do nothing if the unit is invalid
     // or doesn't exist in the current ad layer.
     // Since the WP shortcode pattern is running and trying to replace
     // our shortcode, we need to return the ad and what to replace there,
     // so we set the echo value to false for get_ad_unit.
     return Ad_Layers_Ad_Server::instance()->get_ad_unit($atts['unit'], false);
 }
 /**
  * Adds the meta boxes required to manage an ad layer.
  *
  * @param string|array $post_types
  * @param string $context
  * @param string $priority
  */
 public function add_meta_boxes()
 {
     if (!class_exists('Fieldmanager_Field')) {
         return;
     }
     // Add ad units
     $ad_unit_args = array('name' => 'ad_layer_ad_units', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'sortable' => true, 'label' => __('Select one or more ad units.', 'ad-layers'), 'add_more_label' => __('Add an ad unit', 'ad-layers'), 'children' => array('ad_unit' => new Fieldmanager_Select(array('label' => __('Ad Unit', 'ad-layers'), 'options' => Ad_Layers_Ad_Server::instance()->get_ad_units())), 'do_not_render' => new Fieldmanager_Checkbox(__('Do not render the ad on load', 'ad-layers'))));
     $targeting_args = Ad_Layers_Ad_Server::instance()->get_custom_targeting_args('custom_targeting');
     if (!empty($targeting_args)) {
         $ad_unit_args['children']['custom_targeting'] = new Fieldmanager_Group(apply_filters('ad_layers_custom_targeting_ad_unit_args', $targeting_args));
     }
     $fm_ad_units = new Fieldmanager_Group(apply_filters('ad_layers_ad_units_field_args', $ad_unit_args));
     $fm_ad_units->add_meta_box(__('Ad Units', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Add page types
     $fm_page_types = new Fieldmanager_Select(apply_filters('ad_layers_page_types_field_args', array('name' => 'ad_layer_page_types', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more page types to be targeted with this ad layer.', 'ad-layers'), 'add_more_label' => __('Add a page type', 'ad-layers'), 'options' => Ad_Layers::instance()->get_page_types())));
     $fm_page_types->add_meta_box(__('Page Types', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Add taxonomies
     $fm_taxonomies = new Fieldmanager_Select(apply_filters('ad_layers_taxonomies_field_args', array('name' => 'ad_layer_taxonomies', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more optional taxonomies for targeting. Posts with any term in these taxonomies will get the ad layer.', 'ad-layers'), 'add_more_label' => __('Add a taxonomy', 'ad-layers'), 'options' => Ad_Layers::instance()->get_taxonomies())));
     $fm_taxonomies->add_meta_box(__('Taxonomies', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Add post types
     $fm_post_types = new Fieldmanager_Select(apply_filters('ad_layers_post_types_field_args', array('name' => 'ad_layer_post_types', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more optional post types for targeting. Any post of this type will get the ad layer.', 'ad-layers'), 'add_more_label' => __('Add a post type', 'ad-layers'), 'options' => Ad_Layers::instance()->get_post_types())));
     $fm_post_types->add_meta_box(__('Post Types', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Custom targeting variables
     $targeting_args = Ad_Layers_Ad_Server::instance()->get_custom_targeting_args();
     if (!empty($targeting_args)) {
         $fm_custom = new Fieldmanager_Group(apply_filters('ad_layers_custom_targeting_field_args', $targeting_args));
         $fm_custom->add_meta_box(__('Page Level Custom Targeting', 'ad-layers'), $this->post_type, 'normal', 'low');
     }
 }
 /**
  * Setup the singleton.
  *
  * @access public
  */
 public function setup()
 {
     /**
      * Filter the capability required to manage the ad server settings.
      *
      * @param string $capability. Defaults to `manage_options`.
      */
     $this->settings_capability = apply_filters('ad_layers_settings_capability', 'manage_options');
     // Register the settings page
     if (function_exists('fm_register_submenu_page') && current_user_can($this->settings_capability)) {
         fm_register_submenu_page($this->option_name, Ad_Layers::instance()->get_edit_link(), __('Ad Server Settings', 'ad-layers'), null, $this->settings_capability);
     }
     // Hook the ad layer settings page onto Fieldmanager's action.
     add_action('fm_submenu_' . $this->option_name, array($this, 'add_settings_page'));
     // Add the required header and footer setup. May differ for each server.
     add_action('wp_head', array($this, 'header_setup'));
     add_action('wp_footer', array($this, 'footer_setup'));
     // Handle rendering units
     add_action('ad_layers_render_ad_unit', array($this, 'get_ad_unit'));
     // Load the Javascript API early
     add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'), 5);
     // Load current settings
     self::$settings = apply_filters('ad_layers_ad_server_settings', get_option($this->option_name, array()));
     // Allow additional ad servers to be loaded via filter within a theme
     $this->ad_servers = apply_filters('ad_layers_ad_servers', array('Ad_Layers_DFP' => AD_LAYERS_BASE_DIR . '/php/ad-servers/class-ad-layers-dfp.php'));
     // Load ad server classes
     if (!empty($this->ad_servers) && is_array($this->ad_servers)) {
         foreach ($this->ad_servers as $ad_server) {
             if (file_exists($ad_server)) {
                 require_once $ad_server;
             }
         }
     }
     // Set the current ad server class, if defined.
     if (!empty(self::$settings['ad_server']) && class_exists(self::$settings['ad_server'])) {
         $ad_server = new self::$settings['ad_server']();
         $this->ad_server = $ad_server::instance();
     }
 }
 /**
  * Creates a select field to select the ad unit.
  *
  * @access protected
  * @param string $selected_value The currently selected value
  * @return string HTML for the field
  */
 protected function ad_unit_select_field_html($selected_value)
 {
     // Get all ad units in the system.
     // If none exist, display a message.
     // This will also prevent the widget from being saved due to the validation rules in update().
     $no_units = '<p>' . esc_html__('No ad units are currently available.', 'ad-layers') . '</p>';
     $ad_units = Ad_Layers_Ad_Server::instance()->get_ad_units();
     // Build the option list.
     $options = '';
     foreach ($ad_units as $ad_unit) {
         $options .= sprintf('<option value="%s" %s>%s</option>', esc_attr($ad_unit), selected($selected_value, $ad_unit, false), esc_html($ad_unit));
     }
     // Return the complete element.
     return sprintf('<select name="%s" id="%s">%s</select>', $this->get_field_name('ad_unit'), $this->get_field_id('ad_unit'), $options);
 }