PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / class-wpupg-shortcode-takeover.php

class-wpupg-shortcode-takeover.php in WP Ultimate Post Grid 4.1.1, at includes/public/class-wpupg-shortcode-takeover.php

162 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle the grid shortcode takeover.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.8.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public
10 */
11
12 /**
13 * Handle the grid shortcode takeover.
14 *
15 * @since 3.8.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public
18 * @author Brecht Vandersmissen <brecht@bootstrapped.ventures>
19 */
20 class WPUPG_Shortcode_Takeover {
21
22 /**
23 * Register actions and filters.
24 *
25 * @since 3.0.0
26 */
27 public static function init() {
28 add_filter( 'shortcode_atts_wpupg_grid_with_filters', array( __CLASS__, 'shortcode_atts' ), 2, 3 );
29 add_filter( 'shortcode_atts_wpupg_grid', array( __CLASS__, 'shortcode_atts' ), 2, 3 );
30 }
31
32 /**
33 * Filter the grid shortcode attributes.
34 *
35 * @since 3.8.0
36 * @param array $out The output array of shortcode attributes.
37 * @param array $pairs The supported attributes and their defaults.
38 * @param array $atts The user defined shortcode attributes.
39 */
40 public static function shortcode_atts( $out, $pairs, $atts ) {
41 if ( isset( $atts['takeover'] ) ) {
42 switch ( $atts['takeover'] ) {
43 case 'query':
44 $out = self::takeover_query( $out );
45 break;
46 case 'archive':
47 $out = self::takeover_archive( $out, $atts );
48 break;
49 case 'search':
50 $out = self::takeover_search( $out );
51 break;
52 }
53 }
54
55 return $out;
56 }
57
58 /**
59 * Takeover a generic query page.
60 *
61 * @since 3.8.0
62 * @param array $atts The shortcode attributes.
63 */
64 public static function takeover_query( $atts ) {
65 global $wp_query;
66
67 if ( $wp_query && isset( $wp_query->posts ) && is_array( $wp_query->posts ) ) {
68 $post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
69
70 if ( $post_ids ) {
71 $atts[ 'post_id' ] = implode( ';', $post_ids );
72 }
73 }
74
75 return $atts;
76 }
77
78 /**
79 * Takeover an archive page.
80 *
81 * @since 3.8.0
82 * @param array $atts The shortcode attributes.
83 * @param array $raw_atts The raw shortcode attributes.
84 */
85 public static function takeover_archive( $atts, $raw_atts = array() ) {
86 $term = get_queried_object();
87
88 if ( $term && 'WP_Term' === get_class( $term ) ) {
89 if ( self::archive_include_children( $raw_atts ) && is_taxonomy_hierarchical( $term->taxonomy ) ) {
90 $term_ids = array( intval( $term->term_id ) );
91 $children = get_term_children( $term->term_id, $term->taxonomy );
92
93 if ( ! is_wp_error( $children ) ) {
94 $term_ids = array_merge( $term_ids, array_map( 'intval', $children ) );
95 }
96
97 $atts[ $term->taxonomy ] = implode( ';', array_unique( array_filter( $term_ids ) ) );
98 } else {
99 $atts[ $term->taxonomy ] = $term->term_id;
100 }
101 }
102
103 return $atts;
104 }
105
106 /**
107 * Check whether archive takeover should include child terms.
108 *
109 * @since 4.1.1
110 * @param array $atts The raw shortcode attributes.
111 */
112 public static function archive_include_children( $atts ) {
113 if ( ! isset( $atts['archive_include_children'] ) ) {
114 return false;
115 }
116
117 return filter_var( $atts['archive_include_children'], FILTER_VALIDATE_BOOLEAN );
118 }
119
120 /**
121 * Takeover a search page.
122 *
123 * @since 3.8.0
124 * @param array $atts The shortcode attributes.
125 */
126 public static function takeover_search( $atts ) {
127 global $wp_query;
128
129 if ( $wp_query && isset( $wp_query->is_search ) && $wp_query->is_search ) {
130 $search = isset( $wp_query->query_vars ) && isset( $wp_query->query_vars['s'] ) ? $wp_query->query_vars['s'] : false;
131
132 if ( $search ) {
133 $slug = strtolower( trim( $atts['id'] ) );
134 $grid = WPUPG_Grid_Manager::get_grid( $slug );
135
136 if ( $grid ) {
137 $args = array(
138 'post_type' => $grid->post_types(),
139 'post_status' => $grid->post_status(),
140 'nopaging' => true,
141 'posts_per_page' => -1,
142 'ignore_sticky_posts' => true,
143 'fields' => 'ids',
144 's' => $search,
145 );
146
147 $query = new WP_Query( $args );
148 $post_ids = $query->have_posts() ? $query->posts : array();
149
150 if ( $post_ids ) {
151 $atts[ 'post_id' ] = implode( ';', $post_ids );
152 }
153 }
154 }
155 }
156
157 return $atts;
158 }
159 }
160
161 WPUPG_Shortcode_Takeover::init();
162