/**
  * Process the View form shortcode.
  *
  * eg. [wpv-form-view name='my-view' target_id='xx']
  */
 function short_tag_wpv_view_form($atts)
 {
     global $sitepress;
     global $wplogger;
     $wplogger->log($atts);
     apply_filters('wpv_shortcode_debug', 'wpv-form-view', json_encode($atts), '', 'Output shown in the Nested elements section');
     extract(shortcode_atts(array('id' => false, 'name' => false, 'target_id' => 'self'), $atts));
     $id = $this->get_view_id($atts);
     if (empty($id)) {
         return sprintf('<!- %s ->', __('View not found', 'wpv-views'));
     }
     if (empty($target_id) || $target_id == 'self') {
         $target_id = 'self';
         $url = $_SERVER['REQUEST_URI'];
         if (strpos($url, 'wpv-ajax-pagination') !== false || defined('DOING_AJAX') && DOING_AJAX && isset($_REQUEST['action']) && $_REQUEST['action'] == 'wpv_update_parametric_search') {
             if (wp_get_referer()) {
                 $url = wp_get_referer();
             }
         }
     } else {
         if (is_numeric($target_id)) {
             // Adjust for WPML support
             $target_id = apply_filters('translate_object_id', $target_id, 'page', true, null);
             $url = get_permalink($target_id);
         } else {
             return sprintf('<!- %s ->', __('target_id not valid', 'wpv-views'));
         }
     }
     $this->view_used_ids[] = $id;
     array_push($this->view_shortcode_attributes, $atts);
     // Shall we look up in the cache? Is this a Parametric Search View?
     $is_cacheable = $this->is_cacheable($id) && $this->does_view_have_form_controls($id);
     $cache_id = $this->get_cache_id($id);
     $target_id = $atts['target_id'];
     // int, > 0
     if ($is_cacheable) {
         // Is it cached?
         $cached_filter_index = get_option('wpv_transient_viewform_index', array());
         $is_cached = isset($cached_filter_index[$cache_id]) && $cached_filter_index[$cache_id];
         if ($is_cached) {
             $trasient = 'wpv_transient_viewform_' . $cache_id . '_' . $target_id;
             // strlen() <= 45
             $cached_filter = get_transient($trasient);
             if ($cached_filter !== false) {
                 array_pop($this->view_shortcode_attributes);
                 return $cached_filter;
             }
             // We should not reach this line if everything is working fine
             // But if not, let's continue
         }
     }
     //$this->returned_ids_for_parametric_search = array();
     $this->rendering_views_form_in_progress = true;
     $out = '';
     $view_settings = $this->get_view_settings($id);
     if (isset($view_settings['filter_meta_html'])) {
         $this->view_depth++;
         array_push($this->view_ids, $this->current_view);
         $this->current_view = $id;
         // increment the view count.
         if (!isset($this->view_count[$this->view_depth])) {
             $this->view_count[$this->view_depth] = 0;
         }
         $this->view_count[$this->view_depth]++;
         $form_class = array('js-wpv-form-only');
         // Dependant stuff
         $dps_enabled = false;
         $counters_enabled = false;
         if (!isset($view_settings['dps']) || !is_array($view_settings['dps'])) {
             $view_settings['dps'] = array();
         }
         if (isset($view_settings['dps']['enable_dependency']) && $view_settings['dps']['enable_dependency'] == 'enable') {
             $dps_enabled = true;
             $controls_per_kind = wpv_count_filter_controls($view_settings);
             $controls_count = 0;
             $no_intersection = array();
             if (!isset($controls_per_kind['error'])) {
                 // $controls_count = array_sum( $controls_per_kind );
                 $controls_count = $controls_per_kind['cf'] + $controls_per_kind['tax'] + $controls_per_kind['pr'] + $controls_per_kind['search'];
                 if ($controls_per_kind['cf'] > 1 && (!isset($view_settings['custom_fields_relationship']) || $view_settings['custom_fields_relationship'] != 'AND')) {
                     $no_intersection[] = __('custom field', 'wpv-views');
                 }
                 if ($controls_per_kind['tax'] > 1 && (!isset($view_settings['taxonomy_relationship']) || $view_settings['taxonomy_relationship'] != 'AND')) {
                     $no_intersection[] = __('taxonomy', 'wpv-views');
                 }
             } else {
                 $dps_enabled = false;
             }
             if ($controls_count > 0) {
                 if (count($no_intersection) > 0) {
                     $dps_enabled = false;
                 }
             } else {
                 $dps_enabled = false;
             }
         }
         if (!isset($view_settings['filter_meta_html'])) {
             $view_settings['filter_meta_html'] = '';
         }
         if (strpos($view_settings['filter_meta_html'], '%%COUNT%%') !== false) {
             $counters_enabled = true;
         }
         if ($dps_enabled || $counters_enabled) {
             // TODO review this, makes little sense
             if ($dps_enabled) {
                 $form_class[] = 'js-wpv-dps-enabled';
             }
             wpv_filter_extend_query_for_parametric_and_counters(array(), $view_settings, $id);
         } else {
             // Set the force value
             $this->set_force_disable_dependant_parametric_search(true);
         }
         if (!isset($view_settings['dps']['ajax_results'])) {
             $view_settings['dps']['ajax_results'] = 'disable';
         }
         if (!isset($view_settings['dps']['ajax_results_submit'])) {
             $view_settings['dps']['ajax_results_submit'] = 'reload';
         }
         $ajax = $view_settings['dps']['ajax_results'];
         $ajax_submit = $view_settings['dps']['ajax_results_submit'];
         // Disable AJAX results when the target page is set and is not the current one, since there should be no results here whatsoever
         // (and if there are, they belong to a page that should not be targeted by this form)
         $current_page = $this->get_top_current_page();
         if ($target_id != 'self' && (!$current_page || $current_page->ID != $target_id)) {
             $ajax = 'disable';
             $ajax_submit = 'reload';
         }
         if ($ajax == 'enable') {
             $form_class[] = 'js-wpv-ajax-results-enabled';
         } else {
             if ($ajax == 'disable' && $ajax_submit == 'ajaxed') {
                 $form_class[] = 'js-wpv-ajax-results-submit-enabled';
             }
         }
         $page = 1;
         $effect = 'fade';
         $ajax_pre_before = '';
         if (isset($view_settings['dps']['ajax_results_pre_before'])) {
             $ajax_pre_before = esc_attr($view_settings['dps']['ajax_results_pre_before']);
         }
         $ajax_before = '';
         if (isset($view_settings['dps']['ajax_results_before'])) {
             $ajax_before = esc_attr($view_settings['dps']['ajax_results_before']);
         }
         $ajax_after = '';
         if (isset($view_settings['dps']['ajax_results_after'])) {
             $ajax_after = esc_attr($view_settings['dps']['ajax_results_after']);
         }
         //$url = get_permalink($target_id);
         if (isset($sitepress)) {
             // Dirty hack to be able to use the wpml_content_fix_links_to_translated_content() function
             // It will take a string, parse its links based on <a> tag and return the translated link
             // @todo this is not needed anymore, we already translate the $url above
             // on the only case it is a permalink to a given post ID
             $url = '<a href="' . $url . '"></a>';
             $url = wpml_content_fix_links_to_translated_content($url);
             $url = substr($url, 9, -6);
         }
         $out .= '<form autocomplete="off" action="' . $url . '" method="get" class="wpv-filter-form js-wpv-filter-form js-wpv-filter-form-' . $this->get_view_count() . ' ' . implode(' ', $form_class) . '" data-viewnumber="' . $this->get_view_count() . '" data-targetid="' . $target_id . '" data-viewid="' . $id . '">';
         $out .= '<input type="hidden" class="js-wpv-dps-filter-data js-wpv-filter-data-for-this-form" data-action="' . $url . '" data-page="' . $page . '" data-ajax="disable" data-effect="' . $effect . '" data-ajaxprebefore="' . $ajax_pre_before . '" data-ajaxbefore="' . $ajax_before . '" data-ajaxafter="' . $ajax_after . '" />';
         // Set a hidden input for the View attributes, so we can pass them if needed
         $view_attrs = $atts;
         if (isset($view_attrs['name'])) {
             unset($view_attrs['name']);
         }
         if (isset($view_attrs['target_id'])) {
             unset($view_attrs['target_id']);
         }
         if (!empty($view_attrs) && is_array($view_attrs)) {
             $att_data = '';
             foreach ($view_attrs as $att_key => $att_val) {
                 $att_data .= ' data-' . $att_key . '="' . esc_attr($att_val) . '"';
             }
             $out .= '<input type="hidden" class="js-wpv-view-attributes"' . $att_data . ' />';
         }
         // add hidden inputs for any url parameters.
         // We need these for when the form is submitted.
         $url_query = parse_url($url, PHP_URL_QUERY);
         if ($url_query != '') {
             $query_parts = explode('&', $url_query);
             foreach ($query_parts as $param) {
                 $item = explode('=', $param);
                 if (strpos($item[0], 'wpv_') !== 0) {
                     $out .= '<input id="wpv_param_' . $item[0] . '" type="hidden" name="' . $item[0] . '" value="' . $item[1] . '" />' . "\n";
                 }
             }
         }
         $meta_html = $view_settings['filter_meta_html'];
         $fixmatches = '';
         if (preg_match('#\\[wpv-filter-start.*?\\](.*?)\\[\\wpv-filter-end\\]#is', $meta_html, $matches)) {
             $fixmatches = str_replace(' hide="true"', '', $matches[1]);
         } else {
             if (preg_match('#\\[wpv-filter-controls\\](.*?)\\[\\/wpv-filter-controls\\]#is', $meta_html, $matches)) {
                 $fixmatches = str_replace(' hide="true"', '', $matches[0]);
             } elseif (preg_match('#\\[wpv-control.*?\\]#is', $meta_html) || preg_match('#\\[wpv-filter-search-box.*?\\]#is', $meta_html)) {
                 if (preg_match('#\\[wpv-filter-start.*?\\](.*?)\\[\\wpv-filter-end\\]#is', $meta_html, $matches)) {
                     $fixmatches = str_replace(' hide="true"', '', $matches[1]);
                 }
             }
         }
         $out .= wpv_do_shortcode($fixmatches);
         $requires_current_page = false;
         /**
          * wpv_filter_requires_current_page
          *
          * Whether the current View requires the current page for any filter
          *
          * @param $requires_current_page boolean
          * @param $view_settings
          *
          * @since unknown
          */
         $requires_current_page = apply_filters('wpv_filter_requires_current_page', $requires_current_page, $view_settings);
         if ($requires_current_page) {
             $current_post = $this->get_top_current_page();
             if ($current_post && isset($current_post->ID)) {
                 $out .= '<input id="wpv_post_id-' . esc_attr($this->get_view_count()) . '" type="hidden" name="wpv_post_id" value="' . esc_attr($current_post->ID) . '" class="js-wpv-keep-on-clear" />';
             }
         }
         $requires_parent_term = false;
         /**
          * wpv_filter_requires_parent_term
          *
          * Whether the current View is nested and requires the parent term for any filter
          *
          * @param $requires_parent_term boolean
          * @param $view_settings
          *
          * @since unknown
          */
         $requires_parent_term = apply_filters('wpv_filter_requires_parent_term', $requires_parent_term, $view_settings);
         if ($requires_parent_term) {
             $parent_term_id = $this->get_parent_view_taxonomy();
             if ($parent_term_id) {
                 $out .= '<input id="wpv_aux_parent_term_id-' . esc_attr($this->get_view_count()) . '" type="hidden" name="wpv_aux_parent_term_id" value="' . esc_attr($parent_term_id) . '" class="js-wpv-keep-on-clear" />';
             }
         }
         $requires_parent_user = false;
         /**
          * wpv_filter_requires_parent_user
          *
          * Whether the current View is nested and requires the parent user for any filter
          *
          * @param $requires_parent_user boolean
          * @param $view_settings
          *
          * @since unknown
          */
         $requires_parent_user = apply_filters('wpv_filter_requires_parent_user', $requires_parent_user, $view_settings);
         if ($requires_parent_user) {
             $parent_user_id = $this->get_parent_view_user();
             if ($parent_user_id) {
                 $out .= '<input id="wpv_aux_parent_user_id-' . esc_attr($this->get_view_count()) . '" type="hidden" name="wpv_aux_parent_user_id" value="' . esc_attr($parent_user_id) . '" class="js-wpv-keep-on-clear" />';
             }
         }
         $out .= '</form>';
         $this->current_view = array_pop($this->view_ids);
         if ($this->current_view == null) {
             $this->current_view = $id;
         }
         $this->view_depth--;
         // Update Views cache if applicable
         if ($is_cacheable) {
             // Update Views Filter Content Cache
             $trasient = 'wpv_transient_viewform_' . $cache_id;
             $is_stored = set_transient($trasient, $out, DAY_IN_SECONDS);
             // Update Views Filter Index
             if ($is_stored === true) {
                 $cached_filter_index = get_option('wpv_transient_viewform_index', array());
                 $cached_filter_index = $cached_filter_index === false ? array() : $cached_filter_index;
                 $cached_filter_index[$cache_id] = true;
                 update_option('wpv_transient_viewform_index', $cached_filter_index);
             }
         }
     }
     array_pop($this->view_shortcode_attributes);
     //$this->returned_ids_for_parametric_search = array();
     $this->rendering_views_form_in_progress = false;
     return $out;
 }
Example #2
0
 /**
  * Process the view shortcode.
  *
  * eg. [wpv-form-view name='my-view' target_id='xx']
  */
 function short_tag_wpv_view_form($atts)
 {
     global $sitepress;
     global $wplogger;
     $wplogger->log($atts);
     apply_filters('wpv_shortcode_debug', 'wpv-form-view', json_encode($atts), '', 'Output shown in the Nested elements section');
     extract(shortcode_atts(array('id' => false, 'name' => false, 'target_id' => 'self'), $atts));
     $id = $this->get_view_id($atts);
     if (empty($id)) {
         return sprintf('<!- %s ->', __('View not found', 'wpv-views'));
     }
     if (empty($target_id) || $target_id == 'self') {
         $target_id = 'self';
         $url = $_SERVER['REQUEST_URI'];
         if (strpos($url, 'wpv-ajax-pagination') !== false || defined('DOING_AJAX') && DOING_AJAX && isset($_REQUEST['action']) && $_REQUEST['action'] == 'wpv_update_filter_form') {
             if (wp_get_referer()) {
                 $url = wp_get_referer();
             }
         }
     } else {
         if (is_numeric($target_id)) {
             $url = get_permalink($target_id);
         } else {
             return sprintf('<!- %s ->', __('target_id not valid', 'wpv-views'));
         }
     }
     $this->view_used_ids[] = $id;
     array_push($this->view_shortcode_attributes, $atts);
     //$this->returned_ids_for_parametric_search = array();
     $this->rendering_views_form_in_progress = true;
     $out = '';
     $view_settings = $this->get_view_settings($id);
     if (isset($view_settings['filter_meta_html'])) {
         $this->view_depth++;
         array_push($this->view_ids, $this->current_view);
         $this->current_view = $id;
         // increment the view count.
         if (!isset($this->view_count[$this->view_depth])) {
             $this->view_count[$this->view_depth] = 0;
         }
         $this->view_count[$this->view_depth]++;
         $form_class = array('js-wpv-form-only');
         // Dependant stuff
         $dps_enabled = false;
         $counters_enabled = false;
         if (!isset($view_settings['dps']) || !is_array($view_settings['dps'])) {
             $view_settings['dps'] = array();
         }
         if (isset($view_settings['dps']['enable_dependency']) && $view_settings['dps']['enable_dependency'] == 'enable') {
             $dps_enabled = true;
             $controls_per_kind = wpv_count_filter_controls($view_settings);
             $controls_count = 0;
             $no_intersection = array();
             if (!isset($controls_per_kind['error'])) {
                 // $controls_count = array_sum( $controls_per_kind );
                 $controls_count = $controls_per_kind['cf'] + $controls_per_kind['tax'] + $controls_per_kind['pr'] + $controls_per_kind['search'];
                 if ($controls_per_kind['cf'] > 1 && (!isset($view_settings['custom_fields_relationship']) || $view_settings['custom_fields_relationship'] != 'AND')) {
                     $no_intersection[] = __('custom field', 'wpv-views');
                 }
                 if ($controls_per_kind['tax'] > 1 && (!isset($view_settings['taxonomy_relationship']) || $view_settings['taxonomy_relationship'] != 'AND')) {
                     $no_intersection[] = __('taxonomy', 'wpv-views');
                 }
             } else {
                 $dps_enabled = false;
             }
             if ($controls_count > 0) {
                 if (count($no_intersection) > 0) {
                     $dps_enabled = false;
                 }
             } else {
                 $dps_enabled = false;
             }
         }
         if (!isset($view_settings['filter_meta_html'])) {
             $view_settings['filter_meta_html'] = '';
         }
         if (strpos($view_settings['filter_meta_html'], '%%COUNT%%') !== false) {
             $counters_enabled = true;
         }
         if ($dps_enabled || $counters_enabled) {
             // TODO review this, makes little sense
             if ($dps_enabled) {
                 $form_class[] = 'js-wpv-dps-enabled';
             }
             wpv_filter_extend_query_for_parametric_and_counters(array(), $view_settings, $id);
         } else {
             // Set the force value
             $this->set_force_disable_dependant_parametric_search(true);
         }
         if (!isset($view_settings['dps']['ajax_results'])) {
             $view_settings['dps']['ajax_results'] = 'disable';
         }
         if (!isset($view_settings['dps']['ajax_results_submit'])) {
             $view_settings['dps']['ajax_results_submit'] = 'reload';
         }
         $ajax = $view_settings['dps']['ajax_results'];
         $ajax_submit = $view_settings['dps']['ajax_results_submit'];
         // Disable AJAX results when the target page is set and is not the current one, since there should be no results here whatsoever
         // (and if there are, they belong to a page that should not be targeted by this form)
         $current_page = $this->get_top_current_page();
         if ($target_id != 'self' && (!$current_page || $current_page->ID != $target_id)) {
             $ajax = 'disable';
             $ajax_submit = 'reload';
         }
         if ($ajax == 'enable') {
             $form_class[] = 'js-wpv-ajax-results-enabled';
         } else {
             if ($ajax == 'disable' && $ajax_submit == 'ajaxed') {
                 $form_class[] = 'js-wpv-ajax-results-submit-enabled';
             }
         }
         $page = 1;
         $effect = 'fade';
         $ajax_pre_before = '';
         if (isset($view_settings['dps']['ajax_results_pre_before'])) {
             $ajax_pre_before = esc_attr($view_settings['dps']['ajax_results_pre_before']);
         }
         $ajax_before = '';
         if (isset($view_settings['dps']['ajax_results_before'])) {
             $ajax_before = esc_attr($view_settings['dps']['ajax_results_before']);
         }
         $ajax_after = '';
         if (isset($view_settings['dps']['ajax_results_after'])) {
             $ajax_after = esc_attr($view_settings['dps']['ajax_results_after']);
         }
         //$url = get_permalink($target_id);
         if (isset($sitepress)) {
             // Dirty hack to be able to use the wpml_content_fix_links_to_translated_content() function
             // It will take a string, parse its links based on <a> tag and return the translated link
             // TODO use the icl_object_id function instead
             $url = '<a href="' . $url . '"></a>';
             $url = wpml_content_fix_links_to_translated_content($url);
             $url = substr($url, 9, -6);
         }
         $out .= '<form autocomplete="off" action="' . $url . '" method="get" class="wpv-filter-form js-wpv-filter-form js-wpv-filter-form-' . $this->get_view_count() . ' ' . implode(' ', $form_class) . '" data-viewnumber="' . $this->get_view_count() . '" data-targetid="' . $target_id . '" data-viewid="' . $id . '">';
         $out .= '<input type="hidden" class="js-wpv-dps-filter-data js-wpv-filter-data-for-this-form" data-action="' . $url . '" data-page="' . $page . '" data-ajax="disable" data-effect="' . $effect . '" data-ajaxprebefore="' . $ajax_pre_before . '" data-ajaxbefore="' . $ajax_before . '" data-ajaxafter="' . $ajax_after . '" />';
         // Set a hidden input for the View attributes, so we can pass them if needed
         $view_attrs = $atts;
         if (isset($view_attrs['name'])) {
             unset($view_attrs['name']);
         }
         if (isset($view_attrs['target_id'])) {
             unset($view_attrs['target_id']);
         }
         if (!empty($view_attrs) && is_array($view_attrs)) {
             $att_data = '';
             foreach ($view_attrs as $att_key => $att_val) {
                 $att_data .= ' data-' . $att_key . '="' . esc_attr($att_val) . '"';
             }
             $out .= '<input type="hidden" class="js-wpv-view-attributes"' . $att_data . ' />';
         }
         // add hidden inputs for any url parameters.
         // We need these for when the form is submitted.
         $url_query = parse_url($url, PHP_URL_QUERY);
         if ($url_query != '') {
             $query_parts = explode('&', $url_query);
             foreach ($query_parts as $param) {
                 $item = explode('=', $param);
                 if (strpos($item[0], 'wpv_') !== 0) {
                     $out .= '<input id="wpv_param_' . $item[0] . '" type="hidden" name="' . $item[0] . '" value="' . $item[1] . '" />' . "\n";
                 }
             }
         }
         $meta_html = $view_settings['filter_meta_html'];
         $fixmatches = '';
         if (preg_match('#\\[wpv-filter-start.*?\\](.*?)\\[\\wpv-filter-end\\]#is', $meta_html, $matches)) {
             $fixmatches = str_replace(' hide="true"', '', $matches[1]);
         } else {
             if (preg_match('#\\[wpv-filter-controls\\](.*?)\\[\\/wpv-filter-controls\\]#is', $meta_html, $matches)) {
                 $fixmatches = str_replace(' hide="true"', '', $matches[0]);
             } elseif (preg_match('#\\[wpv-control.*?\\]#is', $meta_html) || preg_match('#\\[wpv-filter-search-box.*?\\]#is', $meta_html)) {
                 if (preg_match('#\\[wpv-filter-start.*?\\](.*?)\\[\\wpv-filter-end\\]#is', $meta_html, $matches)) {
                     $fixmatches = str_replace(' hide="true"', '', $matches[1]);
                 }
             }
         }
         $out .= wpv_do_shortcode($fixmatches);
         $out .= '</form>';
         $this->current_view = array_pop($this->view_ids);
         if ($this->current_view == null) {
             $this->current_view = $id;
         }
         $this->view_depth--;
     }
     array_pop($this->view_shortcode_attributes);
     //$this->returned_ids_for_parametric_search = array();
     $this->rendering_views_form_in_progress = false;
     return $out;
 }