Advertisement
emcniece

FWP Customization - Sort not working

Apr 29th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. * FacetWP Modifications
  6. *
  7. **/
  8.  
  9. /*=====================================================
  10. =            CAF Settings Scrolltop option            =
  11. =====================================================*/
  12.  
  13. add_action('wp_head', 'caf_fwp_scrolltop_js');
  14. function caf_fwp_scrolltop_js(){
  15.     $CAF_Settings = get_option('CAF_Settings', array());
  16.  
  17.     if( $CAF_Settings['opt-caf-fwp-scrolltop-refresh']){
  18.         echo '<script type="text/javascript">var CAF_FWP_scrolltop = true;</script>';
  19.     } else{
  20.         echo '<script type="text/javascript">var CAF_FWP_scrolltop = false;</script>';
  21.     }
  22.  
  23. }
  24.  
  25. /*===========================================
  26. =            Altering Pager Text            =
  27. ===========================================*/
  28.  
  29. add_filter( 'facetwp_pager_html', 'caf_facetwp_pager_html', 10, 2 );
  30. function caf_facetwp_pager_html( $output, $params ) {
  31.     global $CAF_Settings;
  32.  
  33.     if( $first_text = $CAF_Settings['opt-caf-fwp-pager-first-text'])
  34.         $output = str_replace('&lt;&lt;', $first_text, $output);
  35.  
  36.     if( $last_text = $CAF_Settings['opt-caf-fwp-pager-last-text'])
  37.         $output = str_replace('&gt;&gt;', $last_text, $output);
  38.  
  39.     return $output;
  40. }
  41.  
  42. /*==============================================================
  43. =            Alter Sort Results, Add Items Per Page            =
  44. ==============================================================*/
  45.  
  46. //add_filter( 'facetwp_sort_html', 'caf_facetwp_sort_html', 10, 2 );
  47. function caf_facetwp_sort_html( $html, $params ) {
  48.     global $CAF_Settings;
  49.  
  50.     $html = '<select class="facetwp-sort-select">';
  51.     foreach ( $params['sort_options'] as $key => $atts ) {
  52.         $html .= '<option value="' . $key . '">' . $atts['label'] . '</option>';
  53.     }
  54.     $html .= '</select>';
  55.  
  56.     return $html;
  57. }
  58.  
  59. /*=======================================================
  60. =            Custom Items Per Page Shortcode            =
  61. =======================================================*/
  62. add_shortcode( 'caf_facetwp','caf_facetwp_custom' );
  63. function caf_facetwp_custom( $atts ) {
  64.     global $CAF_Settings;
  65.     $html = '';
  66.  
  67.     $atts = extract( shortcode_atts( array(
  68.         'itemsperpage'=>false
  69.     ),$atts ) );
  70.  
  71.  
  72.     if( $itemsperpage){
  73.         $html .= '<div class="facetwp-itemsperpage caf-facetwp"><select class="caf-fwp-items-per-page">';
  74.         foreach($CAF_Settings['opt-caf-fwp-items-per-page'] as $item){
  75.             $val = explode(':', $item);
  76.             $label = isset($val[1]) ? $val[1] : $val[0];
  77.             $html .= '<option value="'.$val[0].'">'.$label.'</option>';
  78.         }
  79.         $html .= '</select></div>';
  80.     }
  81.  
  82.     return $html;
  83.     // do shortcode actions here
  84. }
  85.  
  86. /*=============================================
  87. =            Altering Sort Options            =
  88. =============================================*/
  89. /*
  90. These fields are JSON-encoded and entered in the Redux Options framework
  91. {"price_desc": { "label": "Price (Lowest)", "query_args": { "orderby": "meta_value_num", "meta_key":"caf_regular_price", "order": "DESC" } } }
  92. {"price_asc": { "label": "Price (Highest)", "query_args": { "orderby": "meta_value_num", "meta_key":"caf_regular_price", "order": "ASC" } } }
  93. {"year_desc": { "label": "Year (Oldest)", "query_args": { "orderby": "meta_value", "meta_key":"caf_year", "order": "DESC" } } }
  94. {"year_asc": { "label": "Year (Newest)", "query_args": { "orderby": "meta_value", "meta_key":"caf_year", "order": "ASC" } } }
  95. */
  96.  
  97. add_filter( 'facetwp_sort_options', 'caf_facetwp_sort_options', 10, 2 );
  98. function caf_facetwp_sort_options( $options, $params ) {
  99.     global $CAF_Settings;
  100.  
  101.     if( !empty($CAF_Settings['opt-caf-fwp-sort-options'])){
  102.         $options = array();
  103.  
  104.         foreach($CAF_Settings['opt-caf-fwp-sort-options'] as $opt){
  105.             if( $option = json_decode($opt, true) ){
  106.  
  107.                 // Get first key of object
  108.                 $keys = array_keys($option);
  109.  
  110.                 // Add to options array
  111.                 $options[$keys[0]] = $option[$keys[0]];
  112.  
  113.             } // if decode successful
  114.  
  115.         } // foreach search option
  116.     } // if search options set
  117.  
  118.     echo '<pre style="height:120px;overflow-y:scroll;resize:both;">facetwp_sort_options(): $options = '.print_r($options, true).'</pre>';
  119.     return $options;
  120. }
  121.  
  122. /*======================================================
  123. =            Alter Query for Items per Page            =
  124. ======================================================*/
  125.  
  126. add_filter( 'facetwp_query_args', 'caf_facetwp_query_args', 10, 2 );
  127. function caf_facetwp_query_args( $query_args, $class ) {
  128.     if ( isset( $class->http_params['per_page'] ) ) {
  129.         $query_args['posts_per_page'] = (int) $class->http_params['per_page'];
  130.     }
  131.     echo '<pre style="height:120px;overflow-y:scroll;resize:both;">facetwp_query_args(): $query_args = '.print_r($query_args, true).'</pre>';
  132.     return $query_args;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement