/**
  * Process view $settings array, return HTML output
  *
  * @param string $id       The current view id
  * @param array  $settings The settings array
  * @param array  $pargs    The pagination settings array
  *
  * @return string HTML output of Content View
  */
 static function view_process_settings($id, $settings, $pargs = array())
 {
     if (empty($settings)) {
         return __('Empty settings', PT_CV_DOMAIN);
     }
     global $pt_cv_glb, $pt_cv_id;
     $view_id = !empty($id) ? $id : PT_CV_Functions::string_random();
     // Init arrays
     if (!isset($pt_cv_glb[$view_id])) {
         $pt_cv_glb[$view_id] = array();
     }
     if (!isset($pt_cv_glb['processed_view'])) {
         $pt_cv_glb['processed_view'] = array();
     }
     /**
      * 1. Get View settings
      */
     $view_settings = array();
     foreach ($settings as $key => $value) {
         $view_settings[$key] = esc_sql($value);
     }
     $pt_cv_glb[$view_id]['view_settings'] = $view_settings;
     // Check duplicated View
     // (Same View ID but different shortcode parameters => consider as 2 different Views)
     if (empty($pargs) && apply_filters(PT_CV_PREFIX_ . 'check_duplicate', 0, $view_id, $view_settings)) {
         $sc_params = isset($pt_cv_glb[$view_id]['shortcode_params']) ? $pt_cv_glb[$view_id]['shortcode_params'] : PT_CV_Functions::string_random();
         $vid = $view_id . '-' . md5(serialize($sc_params));
         if (!empty($pt_cv_glb['processed_view'][$vid])) {
             return '';
         } else {
             $pt_cv_glb['processed_view'][$vid] = 1;
         }
     }
     // Get content type
     $content_type = apply_filters(PT_CV_PREFIX_ . 'content_type', PT_CV_Functions::setting_value(PT_CV_PREFIX . 'content-type', $view_settings), $id);
     $pt_cv_glb[$view_id]['content_type'] = $content_type;
     // Get view type
     $view_type = PT_CV_Functions::setting_value(PT_CV_PREFIX . 'view-type', $view_settings);
     $pt_cv_glb[$view_id]['view_type'] = $view_type;
     // If is pagination request
     $session_id = $pargs && isset($pargs['session_id']) ? $pargs['session_id'] : 0;
     // Store main View ID
     // If parent View is not finished
     if (!isset($pt_cv_glb[$pt_cv_id]['finished'])) {
         $pt_cv_main_id = !empty($pt_cv_id) ? $pt_cv_id : $view_id;
     } else {
         $pt_cv_main_id = $view_id;
     }
     if ($session_id) {
         if (empty($pt_cv_id)) {
             $pt_cv_id = $session_id;
         }
         $session_data = array_merge(array('$args' => '', '$dargs' => ''), false === ($saved_settings = get_transient(PT_CV_PREFIX . 'view-data-' . $session_id)) ? array() : $saved_settings);
         $args = $session_data['$args'];
         $dargs = $session_data['$dargs'];
     } else {
         // Assign view id as current View
         $pt_cv_id = $session_id = $view_id;
         // Store settings
         set_transient(PT_CV_PREFIX . 'view-settings-' . $session_id, $settings, 7 * DAY_IN_SECONDS);
     }
     // Extract Query & Display settings from settings array
     if (empty($args) || empty($dargs)) {
         $dargs = apply_filters(PT_CV_PREFIX_ . 'all_display_settings', PT_CV_Functions::view_display_settings($view_type, $dargs));
         $args = apply_filters(PT_CV_PREFIX_ . 'query_parameters', PT_CV_Functions::view_filter_settings($content_type, $view_settings));
         // Store view data
         set_transient(PT_CV_PREFIX . 'view-data-' . $session_id, array('$args' => $args, '$dargs' => $dargs), 7 * DAY_IN_SECONDS);
     }
     // Pagination settings
     PT_CV_Functions::view_get_pagination_settings($dargs, $args, $pargs);
     // Update global query parameters variable
     $pt_cv_glb[$view_id]['dargs'] = $dargs;
     $pt_cv_glb[$view_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);
     // Return error message
     if ($error) {
         return implode('</p><p>', $error);
     }
     /**
      * 2. Output Items
      */
     $pt_query = array();
     // What kind of content to display
     $pt_cv_glb[$view_id]['display_what'] = apply_filters(PT_CV_PREFIX_ . 'display_what', 'post');
     if ($pt_cv_glb[$view_id]['display_what'] === 'post') {
         // Display posts
         // Get $content_items & $pt_query
         extract(self::get_posts_list($args, $view_type));
     } else {
         // Display categories...
         $content_items = apply_filters(PT_CV_PREFIX_ . 'view_content', array());
     }
     // Restore main View id
     $pt_cv_id = $pt_cv_main_id;
     /**
      * 3. Output Pagination
      */
     $current_page = self::get_current_page($pargs);
     $html = PT_CV_Html::content_items_wrap($content_items, $current_page, $args['posts_per_page'], $view_id);
     // Append Pagination HTML if this is first page, or not Ajax calling
     if ($pt_query && $args['posts_per_page'] > 0 && PT_CV_Functions::is_pagination($dargs, $current_page)) {
         // Total post founds
         $found_posts = 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
         $max_num_pages = ceil($total_items / $args['posts_per_page']);
         // Output pagination
         $html .= "\n" . PT_CV_Html::pagination_output($max_num_pages, $current_page, $session_id);
     }
     // Reset View ID
     $pt_cv_glb[$pt_cv_id]['finished'] = 1;
     return $html;
 }
Example #2
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;
 }