/**
  * Get singleton instance of class
  *
  * @return null|WPSWS_Output
  */
 public static function get()
 {
     if (null === self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 /**
  * This is the default included 'get_posts' webservice
  * This webservice will fetch all posts of set post type
  *
  * @todo
  * - All sorts of security checks
  * - Allow custom query variables in webservice (e.g. custom sorting, posts_per_page, etc.)
  */
 public function get_posts()
 {
     // Check if post type is set
     if (!isset($_GET['post_type'])) {
         WP_Simple_Web_Service::get()->throw_error('No post type set.');
     }
     // Set post type
     $post_type = esc_sql($_GET['post_type']);
     // Global options
     $options = WP_Simple_Web_Service::get()->get_options();
     // Get 'get_posts' options
     $gp_options = array();
     if (isset($options['get_posts'])) {
         $gp_options = $options['get_posts'];
     }
     // Fix scenario where there are no settings for given post type
     if (!isset($gp_options[$post_type])) {
         $gp_options[$post_type] = array();
     }
     // Setup options
     $pt_options = wp_parse_args($gp_options[$post_type], $this->get_default_settings());
     // Check if post type is enabled
     if ('false' == $pt_options['enabled']) {
         WP_Simple_Web_Service::get()->throw_error('Post Type not supported.');
     }
     // Setup default query vars
     $default_query_arguments = array('posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'title');
     // Get query vars
     $query_vars = array();
     if (isset($_GET['qv'])) {
         $query_vars = $_GET['qv'];
     }
     // Merge query vars
     $query_vars = wp_parse_args($query_vars, $default_query_arguments);
     // Set post type
     $query_vars['post_type'] = $post_type;
     // Get posts
     $posts = get_posts($query_vars);
     // Post data to show - this will be manageble at some point
     $show_post_data_fields = array('ID', 'post_title', 'post_content', 'post_date');
     // Post meta data to show - this will be manageble at some point
     $show_post_meta_data_fields = array('ssm_supermarkt', 'ssm_adres');
     // Data array
     $return_data = array();
     // Loop through posts
     foreach ($posts as $post) {
         // Get post customs
         $post_custom = get_post_custom($post->ID);
         $data = array();
         // Add regular post fields data array
         foreach ($pt_options['fields'] as $show_post_data_field) {
             $post_field_value = $post->{$show_post_data_field};
             // Fetch thumbnail
             if ('thumbnail' == $show_post_data_field) {
                 $post_field_value = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
             }
             // Set post field value
             $data[$show_post_data_field] = $post_field_value;
         }
         // Add post meta fields to data array
         foreach ($pt_options['custom'] as $show_post_meta_data_field) {
             $meta_field_value = get_post_meta($post->ID, $show_post_meta_data_field, true);
             if ($meta_field_value != '') {
                 $data[$show_post_meta_data_field] = $meta_field_value;
             }
         }
         $return_data[] = $data;
     }
     $return_data = apply_filters('wpsws_get_posts_data', $return_data, $post_type);
     WPSWS_Output::get()->output($return_data);
 }