Example #1
0
 /**
  * Process view settings, return HTML output
  *
  * @param string $view_id  View id
  * @param array  $settings The settings
  * @param array  $pargs    The pagination settings
  *
  * @return string HTML output of Content View
  */
 static function view_process_settings($view_id, $settings, $pargs = array())
 {
     if (empty($settings[PT_CV_PREFIX . 'view-type'])) {
         return sprintf(__('Empty settings (View %s is not existed)', 'content-views-query-and-display-post-page'), "<strong>{$view_id}</strong>");
     }
     // Check duplicated
     if (self::duplicate_process($view_id, $settings)) {
         return '';
     }
     global $pt_cv_glb, $pt_cv_id;
     $pt_cv_id = $view_id;
     if (!isset($pt_cv_glb)) {
         $pt_cv_glb = array();
     }
     if (!isset($pt_cv_glb[$pt_cv_id])) {
         $pt_cv_glb[$pt_cv_id] = array();
     }
     // Get View settings
     $view_settings = array();
     foreach ($settings as $key => $value) {
         $view_settings[$key] = esc_sql($value);
     }
     $pt_cv_glb[$pt_cv_id]['view_settings'] = $view_settings;
     $content_type = PT_CV_Functions::setting_value(PT_CV_PREFIX . 'content-type', $view_settings);
     $view_type = PT_CV_Functions::setting_value(PT_CV_PREFIX . 'view-type', $view_settings);
     $current_page = self::get_current_page($pargs);
     $pt_cv_glb[$pt_cv_id]['content_type'] = $content_type;
     $pt_cv_glb[$pt_cv_id]['view_type'] = $view_type;
     $pt_cv_glb[$pt_cv_id]['current_page'] = $current_page;
     // Keep current ID
     $pt_cv_cur_id = $view_id;
     if (defined('PT_CV_DOING_PAGINATION')) {
         $session_data = self::get_session(PT_CV_PREFIX . 'view-data-' . $view_id, array('args' => '', 'dargs' => ''));
         $args = $session_data['args'];
         $dargs = $session_data['dargs'];
     }
     if (empty($args) || empty($dargs)) {
         $dargs = PT_CV_Functions::view_display_settings($view_type, $dargs);
         $args = PT_CV_Functions::view_filter_settings($content_type, $view_settings);
         // Store view data before get pagination settings
         self::set_session(PT_CV_PREFIX . 'view-data-' . $view_id, array('args' => $args, 'dargs' => $dargs));
     }
     // Pagination settings
     PT_CV_Functions::view_get_pagination_settings($dargs, $args, $pargs);
     // Apply filter
     $dargs = apply_filters(PT_CV_PREFIX_ . 'all_display_settings', $dargs);
     $args = apply_filters(PT_CV_PREFIX_ . 'query_parameters', $args);
     // Update global query parameters variable
     $pt_cv_glb[$pt_cv_id]['dargs'] = $dargs;
     $pt_cv_glb[$pt_cv_id]['args'] = $args;
     do_action(PT_CV_PREFIX_ . 'add_global_variables');
     // Validate settings, if some required parameters are missing, show error and exit
     $error = apply_filters(PT_CV_PREFIX_ . 'validate_settings', array(), $args);
     if ($error) {
         return implode('</p><p>', $error);
     }
     $content_items = $pt_query = $empty_result = null;
     // What kind of content to display
     $pt_cv_glb[$pt_cv_id]['display_what'] = apply_filters(PT_CV_PREFIX_ . 'display_what', 'post');
     if ($pt_cv_glb[$pt_cv_id]['display_what'] === 'post') {
         extract(self::get_posts_list($args, $view_type));
     } else {
         $content_items = apply_filters(PT_CV_PREFIX_ . 'view_content', array());
     }
     // Restore current ID
     $pt_cv_id = $pt_cv_cur_id;
     // Hide empty result
     if (apply_filters(PT_CV_PREFIX_ . 'hide_empty_result', false) && $empty_result) {
         $html = '';
     } else {
         // Wrap items
         $html = PT_CV_Html::content_items_wrap($content_items, $current_page, $args['posts_per_page'], $view_id);
         // Show pagination
         if ($pt_query && PT_CV_Functions::nonajax_or_firstpage($dargs, $current_page)) {
             // Save settings for reusing in pagination
             self::set_session(PT_CV_PREFIX . 'view-settings-' . $view_id, $settings);
             // Total post founds
             $found_posts = (int) apply_filters(PT_CV_PREFIX_ . 'found_posts', $pt_query->found_posts);
             // Total number of items
             $total_items = $args['limit'] > 0 && $found_posts > $args['limit'] ? $args['limit'] : $found_posts;
             // Total number of pages
             $items_per_page = (int) PT_CV_Functions::setting_value(PT_CV_PREFIX . 'pagination-items-per-page', $view_settings);
             $max_num_pages = ceil($total_items / $items_per_page);
             // Output pagination
             if ((int) $max_num_pages > 0) {
                 $html .= "\n" . PT_CV_Html::pagination_output($max_num_pages, $current_page, $view_id);
             }
         }
     }
     return $html;
 }