/**
  * Suggest
  *
  * Echo a JSON-ified array of posts of the given post-type and
  * the requested search-term and then die silently
  */
 public static function suggest()
 {
     $json = new MslsJson();
     if (filter_has_var(INPUT_POST, 'blog_id')) {
         switch_to_blog(filter_input(INPUT_POST, 'blog_id', FILTER_SANITIZE_NUMBER_INT));
         $args = array('orderby' => 'name', 'order' => 'ASC', 'number' => 10, 'hide_empty' => 0);
         if (filter_has_var(INPUT_POST, 's')) {
             $args['s'] = sanitize_text_field(filter_input(INPUT_POST, 's'));
         }
         /**
          * Overrides the query-args for the suggest fields
          * @since 0.9.9
          * @param array $args
          */
         $args = (array) apply_filters('msls_post_tag_suggest_args', $args);
         foreach (get_terms(sanitize_text_field(filter_input(INPUT_POST, 'post_type')), $args) as $term) {
             /**
              * Manipulates the term object before using it
              * @since 0.9.9
              * @param StdClass $term
              */
             $term = apply_filters('msls_post_tag_suggest_term', $term);
             if (is_object($term)) {
                 $json->add($term->term_id, $term->name);
             }
         }
         restore_current_blog();
     }
     wp_die($json->encode());
 }
 /**
  * Verify the add- and the get-methods
  * @covers MslsJson::add
  * @covers MslsJson::get
  * @covers MslsJson::compare
  */
 function test_add_get_methods()
 {
     $obj = new MslsJson();
     $obj->add(null, 'Test 3')->add('2', 'Test 2')->add(1, 'Test 1');
     $this->assertEquals(array(array('value' => 1, 'label' => 'Test 1'), array('value' => 2, 'label' => 'Test 2'), array('value' => 0, 'label' => 'Test 3')), $obj->get());
     return $obj;
 }
Example #3
0
 /**
  * Suggest
  *
  * Echo a JSON-ified array of posts of the given post-type and
  * the requested search-term and then die silently
  */
 public static function suggest()
 {
     $json = new MslsJson();
     if (filter_has_var(INPUT_POST, 'blog_id')) {
         switch_to_blog(filter_input(INPUT_POST, 'blog_id', FILTER_SANITIZE_NUMBER_INT));
         $args = array('post_status' => get_post_stati(array('internal' => '')), 'posts_per_page' => 10);
         if (filter_has_var(INPUT_POST, 'post_type')) {
             $args['post_type'] = sanitize_text_field(filter_input(INPUT_POST, 'post_type'));
         }
         if (filter_has_var(INPUT_POST, 's')) {
             $args['s'] = sanitize_text_field(filter_input(INPUT_POST, 's'));
         }
         /**
          * Overrides the query-args for the suggest fields in the MetaBox
          * @since 0.9.9
          *
          * @param array $args
          */
         $args = (array) apply_filters('msls_meta_box_suggest_args', $args);
         $my_query = new WP_Query($args);
         while ($my_query->have_posts()) {
             $my_query->the_post();
             /**
              * Manipulates the WP_Post object before using it
              * @since 0.9.9
              *
              * @param WP_Post $post
              */
             $my_query->post = apply_filters('msls_meta_box_suggest_post', $my_query->post);
             if (is_object($my_query->post)) {
                 $json->add(get_the_ID(), get_the_title());
             }
         }
         wp_reset_postdata();
         restore_current_blog();
     }
     wp_die($json->encode());
 }