PluginProbe
WP Ultimate Post Grid / 4.0.1
WP Ultimate Post Grid v4.0.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.0.1, at includes/public/class-wpupg-shortcode-takeover.php

136 lines 3.2 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 <[email protected]>
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 );
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 */
84 public static function takeover_archive( $atts ) {
85 $term = get_queried_object();
86
87 if ( $term && 'WP_Term' === get_class( $term ) ) {
88 $atts[ $term->taxonomy ] = $term->term_id;
89 }
90
91 return $atts;
92 }
93
94 /**
95 * Takeover a search page.
96 *
97 * @since 3.8.0
98 * @param array $atts The shortcode attributes.
99 */
100 public static function takeover_search( $atts ) {
101 global $wp_query;
102
103 if ( $wp_query && isset( $wp_query->is_search ) && $wp_query->is_search ) {
104 $search = isset( $wp_query->query_vars ) && isset( $wp_query->query_vars['s'] ) ? $wp_query->query_vars['s'] : false;
105
106 if ( $search ) {
107 $slug = strtolower( trim( $atts['id'] ) );
108 $grid = WPUPG_Grid_Manager::get_grid( $slug );
109
110 if ( $grid ) {
111 $args = array(
112 'post_type' => $grid->post_types(),
113 'post_status' => $grid->post_status(),
114 'nopaging' => true,
115 'posts_per_page' => -1,
116 'ignore_sticky_posts' => true,
117 'fields' => 'ids',
118 's' => $search,
119 );
120
121 $query = new WP_Query( $args );
122 $post_ids = $query->have_posts() ? $query->posts : array();
123
124 if ( $post_ids ) {
125 $atts[ 'post_id' ] = implode( ';', $post_ids );
126 }
127 }
128 }
129 }
130
131 return $atts;
132 }
133 }
134
135 WPUPG_Shortcode_Takeover::init();
136