/**
  * Test arbitrary context actions given a context and type.
  *
  * @param  string $context The context being tested.
  * @param  string $type The subcontext being tested.
  */
 protected function _context_action_assertions($context, $type)
 {
     $a = new MockAction();
     if ($context) {
         add_action("fm_{$context}", array(&$a, 'action'));
     }
     if ($type) {
         add_action("fm_{$context}_{$type}", array(&$a, 'action'));
     }
     fm_get_context(true);
     fm_trigger_context_action();
     if ($type) {
         // only two events occurred for the hook
         $this->assertEquals(2, $a->get_call_count());
         // only our hooks were called
         $this->assertEquals(array("fm_{$context}_{$type}", "fm_{$context}"), $a->get_tags());
         // The $type should have been passed as args
         $this->assertEquals(array(array($type), array($type)), $a->get_args());
     } elseif ($context) {
         // only one event occurred for the hook
         $this->assertEquals(1, $a->get_call_count());
         // only our hook was called
         $this->assertEquals(array("fm_{$context}"), $a->get_tags());
         // null should have been passed as an arg
         $this->assertEquals(array(array(null)), $a->get_args());
     } else {
         // No event should have fired
         $this->assertEquals(0, $a->get_call_count());
     }
 }
 public function form_element($value = null)
 {
     list($context, $subcontext) = fm_get_context();
     $this->autocomplete_attributes['data-context'] = $context;
     $this->autocomplete_attributes['data-subcontext'] = $subcontext;
     $this->autocomplete_attributes['data-action'] = $this->get_ajax_action($this->name);
     $this->autocomplete_attributes['data-nonce'] = wp_create_nonce('fm_search_nonce');
     $this->autocomplete_attributes['data-args'] = json_encode($this->ajax_args);
     return parent::form_element($value);
 }
 public function setup()
 {
     $this->context = fm_get_context();
     // Support byline types by default
     $this->byline_types = apply_filters('fm_bylines_filter_types', array('author'));
     add_action('init', array($this, 'add_type_query_var'));
     add_action('after_setup_theme', array($this, 'theme_setup'), 20);
     if (is_admin()) {
         if ('post' === $this->context[0]) {
             // Disable bylines on attachments by default.
             if ('attachment' != $this->context[1] || apply_filters('fm_bylines_on_attachments', false)) {
                 add_action('do_meta_boxes', array($this, 'remove_meta_boxes'));
                 add_action("fm_{$this->context[0]}_{$this->context[1]}", array($this, 'add_meta_boxes'));
             }
         }
         // Set the column super early so other plugins can manipulate it using the same hook
         add_filter("manage_{$this->context[1]}_posts_columns", array($this, 'set_posts_columns'), 2, 2);
         add_action("manage_{$this->context[1]}_posts_custom_column", array($this, 'display_byline_type_column'), 10, 2);
     }
     add_filter('template_include', array($this, 'set_byline_type_template'));
     add_action('init', array($this, 'set_byline_rewrite_rules'));
     // Archive titles should display type
     add_filter('wp_title_parts', array($this, 'set_archive_title'));
 }
/**
 * Fire an action for the current Fieldmanager context, if it exists.
 *
 * @see fm_calculate_context() for detail about the values that determine the
 *     name of the action. Two actions are defined, but only one at most fires.
 */
function fm_trigger_context_action()
{
    $calculated_context = fm_get_context();
    if (empty($calculated_context)) {
        return;
    }
    $context = $calculated_context[0];
    if ($type = $calculated_context[1]) {
        /**
         * Fires when a specific Fieldmanager context and type load.
         *
         * The dynamic portions of the hook name, $context and $type, refer to
         * the values returned by fm_calculate_context(). For example, the Edit
         * screen for the Page post type would fire "fm_post_page".
         */
        do_action("fm_{$context}_{$type}");
    } else {
        /**
         * Fires when a specific Fieldmanager context, but not type, loads.
         *
         * The dynamic portion of the hook name, $context, refers to the first
         * value returned by fm_calculate_context(). For example, the Edit User
         * screen would fire "fm_user".
         */
        do_action("fm_{$context}");
    }
}
/**
 * Fire an action for the current Fieldmanager context, if it exists.
 *
 * @see fm_calculate_context() for detail about the values that determine the
 *     name of the action. Two actions are defined, but only one at most fires.
 */
function fm_trigger_context_action()
{
    $calculated_context = fm_get_context();
    if (empty($calculated_context[0])) {
        return;
    }
    list($context, $type) = $calculated_context;
    if ($type) {
        /**
         * Fires when a specific Fieldmanager context and type load.
         *
         * The dynamic portions of the hook name, $context and $type, refer to
         * the values returned by fm_calculate_context(). For example, the Edit
         * screen for the Page post type would fire "fm_post_page".
         *
         * @param string $type The context subtype, e.g. the post type, taxonomy
         *                     name, submenu option name.
         */
        do_action("fm_{$context}_{$type}", $type);
    }
    /**
     * Fires when any Fieldmanager context loads.
     *
     * The dynamic portion of the hook name, $context, refers to the first
     * value returned by fm_calculate_context(). For example, the Edit User
     * screen would fire "fm_user".
     *
     * @param string|null $type The context subtype, e.g. the post type,
     *                          taxonomy name, submenu option name. null if this
     *                          context does not have a subtype.
     */
    do_action("fm_{$context}", $type);
}
 /**
  * Add in a FM_Byline meta box w/ all it's bells and whistles
  * @param string $type
  * @param string. Optional label
  * @param array $args
  */
 function add_byline_meta_box($type = 'author', $label = null, $args = array())
 {
     if (is_admin()) {
         $context = fm_get_context();
         $fm_context = $context[0];
         $fm_context_type = $context[1];
         $label = empty($label) ? fm_bylines_wordify_slug($type) : $label;
         $defaults = array('name' => 'fm_bylines_' . sanitize_title_with_dashes($type), 'limit' => 0, 'add_more_label' => __('Add another', 'fm_bylines'), 'sortable' => true, 'label' => __('Name', 'fm_bylines'), 'children' => array('byline_id' => new Fieldmanager_Autocomplete(array('default_value' => null, 'datasource' => new Fieldmanager_Datasource_Post(array('query_args' => array('post_type' => $this->name, 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'post_status' => 'publish', 'suppress_filters' => false))))), 'fm_byline_type' => new Fieldmanager_Hidden(array('default_value' => sanitize_title_with_dashes($type)))));
         $fm_byline_box = new Fieldmanager_Group(wp_parse_args($args, $defaults));
         if ('post' == $fm_context) {
             $fm_byline_box->add_meta_box($label, $fm_context_type, apply_filters('fm_bylines_' . sanitize_title_with_dashes($type) . '_filter_post_metabox_context', 'normal'), apply_filters('fm_bylines_' . sanitize_title_with_dashes($type) . '_filter_post_metabox_priority', 'default'));
         } elseif ('term' == $fm_context) {
             $fm_byline_box->add_term_form($label, $fm_context_type);
         } elseif ('submenu' == $fm_context) {
             fm_register_submenu_page('fm_bylines_' . sanitize_title_with_dashes($type), apply_filters('fm_bylines_' . sanitize_title_with_dashes($type) . '_filter_metabox_submenu', 'tools.php'), $label);
             $fm_byline_box->activate_submenu_page();
         } elseif ('user' == $fm_context) {
             $fm_byline_box->add_user_form($label);
         } elseif ('quickedit' == $fm_context) {
             $fm_byline_box->add_quickedit_box($label, $fm_context_type, function ($post_id, $data) {
                 return !empty($data['fm_bylines_' . sanitize_title_with_dashes($type)]) ? $data['fm_bylines_' . sanitize_title_with_dashes($type)] : 'not set';
             });
         }
     }
 }
 /**
  * Render form element
  * @param mixed $value
  * @return string HTML
  */
 public function form_element($value = Null)
 {
     if ($this->exact_match) {
         $this->attributes['data-exact-match'] = True;
     }
     if ($this->datasource->use_ajax) {
         $this->attributes['data-action'] = $this->datasource->get_ajax_action($this->name);
         list($context, $subcontext) = fm_get_context();
         $this->attributes['data-context'] = $context;
         $this->attributes['data-subcontext'] = $subcontext;
     } else {
         $this->attributes['data-options'] = htmlspecialchars(json_encode($this->datasource->get_items()));
     }
     $display_value = $this->datasource->get_value($value);
     if ('' == $display_value && !$this->exact_match && !isset($this->datasource->options[$value])) {
         $display_value = $value;
     }
     $element = sprintf('<input class="fm-autocomplete fm-element fm-incrementable" type="text" id="%s" value="%s"%s %s />', esc_attr($this->get_element_id()), esc_attr($display_value), !empty($this->custom_args_js_event) ? ' data-custom-args-js-event="' . esc_attr($this->custom_args_js_event) . '"' : '', $this->get_element_attributes());
     $element .= sprintf('<input class="fm-autocomplete-hidden fm-element" type="hidden" name="%s" value="%s" />', esc_attr($this->get_form_name()), esc_attr($value));
     if (isset($this->show_view_link) && $this->show_view_link) {
         $element .= $this->datasource->get_view_link($value);
     }
     if (isset($this->show_edit_link) && $this->show_edit_link) {
         $element .= $this->datasource->get_edit_link($value);
     }
     return $element;
 }