示例#1
0
 /**
  * Get singleton instance of class
  *
  * @return null|DABBA_Output
  */
 public static function get()
 {
     if (self::$instance == null) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 /**
  * Handle webservice request
  */
 public function handle_request()
 {
     global $wp_query;
     if ($wp_query->get('webservice')) {
         if (!isset($_POST['auth_key']) || $_POST['auth_key'] != DABBA_API_AUTH_KEY) {
             DABBA_API_Output::get()->output(false, 403, 'Unauthorized');
             exit;
         }
         if ($wp_query->get('service') != '') {
             // Check if the action exists
             if (has_action('dabba_api_webservice_' . $wp_query->get('service'))) {
                 // Do action
                 do_action('dabba_api_webservice_' . $wp_query->get('service'));
                 // Bye
                 exit;
             }
         }
         DABBA_API_Output::get()->output(false, 501, 'Not Implemented');
         exit;
     }
 }
 /**
  * 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'])) {
         Dabba_Web_Service::get()->throw_error('No post type set.');
     }
     // Set post type
     $post_type = esc_sql($_GET['post_type']);
     // Global options
     $options = Dabba_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']) {
         Dabba_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) {
         $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;
     }
     DABBA_API_Output::get()->output($return_data);
 }