PluginProbe
Sight – Professional Image Gallery and Portfolio / 1.1.7
Sight – Professional Image Gallery and Portfolio v1.1.7
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
sight / render / sight-load-more.php

sight-load-more.php in Sight – Professional Image Gallery and Portfolio 1.1.7, at render/sight-load-more.php

316 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Portfolio Load More Posts via AJAX.
4 *
5 * @package sight
6 */
7
8 /**
9 * Processing data query for load more
10 *
11 * @param string $method Processing method $wp_query.
12 * @param array $data Data array.
13 */
14 function sight_portfolio_load_more_query_data( $method = 'get', $data = array() ) {
15 global $wp_query;
16
17 $output = array();
18
19 $vars = array(
20 'in_the_loop',
21 'is_single',
22 'is_page',
23 'is_archive',
24 'is_author',
25 'is_category',
26 'is_tag',
27 'is_tax',
28 'is_home',
29 'is_singular',
30 'is_post_query',
31 );
32
33 if ( 'get' === $method ) {
34 $output = $data;
35 }
36
37 foreach ( $vars as $variable ) {
38 if ( ! isset( $wp_query->$variable ) ) {
39 continue;
40 }
41 if ( 'get' === $method ) {
42 $output[ $variable ] = $wp_query->$variable;
43 }
44 if ( ! isset( $data[ $variable ] ) ) {
45 continue;
46 }
47 if ( 'init' === $method ) {
48 $wp_query->$variable = $data[ $variable ];
49 }
50 }
51
52 if ( 'get' === $method ) {
53 $output = apply_filters( 'ajax_query_args', $output );
54 }
55
56 return wp_json_encode( $output );
57 }
58
59 /**
60 * Get load more args.
61 *
62 * @param array $data The data.
63 * @param array $attributes The attributes.
64 * @param array $options The options.
65 */
66 function sight_portfolio_get_load_more_args( $data, $attributes = false, $options = false ) {
67 // Ajax Type.
68 $ajax_type = version_compare( get_bloginfo( 'version' ), '4.7', '>=' ) ? 'ajax_restapi' : 'ajax';
69
70 $ajax_type = apply_filters( 'ajax_load_more_method', $ajax_type );
71
72 $args = array(
73 'type' => $ajax_type,
74 'nonce' => wp_create_nonce(),
75 'url' => admin_url( 'admin-ajax.php' ),
76 'rest_url' => esc_url( get_rest_url( null, '/sight/v1/portfolio-more-posts' ) ),
77 'posts_per_page' => get_query_var( 'posts_per_page' ),
78 'query_data' => sight_portfolio_load_more_query_data( 'get', $data ),
79 'attributes' => wp_json_encode( $attributes ),
80 'options' => wp_json_encode( $options ),
81 'max_num_pages' => $data['max_num_pages'],
82 'pagination_type' => $data['pagination_type'],
83 'translation' => array(
84 'load_more' => esc_html__( 'Load more', 'sight' ),
85 'loading' => esc_html__( 'Loading', 'sight' ),
86 ),
87 );
88
89 return $args;
90 }
91
92 /**
93 * Fires after the query variable object is created, but before the actual query is run.
94 *
95 * @param object $wp_query WP Query.
96 */
97 function sight_portfolio_pre_get_posts( &$wp_query ) {
98
99 if ( isset( $wp_query->query['is_sight_query'] ) ) {
100 $offset = (int) $wp_query->get( 'offset' );
101 $paged = (int) $wp_query->get( 'paged' );
102 $posts_per_page = (int) $wp_query->get( 'posts_per_page' );
103
104 if ( $wp_query->is_paged ) {
105 $page_offset = $offset + ( ( $paged - 1 ) * $posts_per_page );
106
107 $wp_query->set( 'offset', $page_offset );
108 } else {
109 $wp_query->set( 'offset', $offset );
110 }
111 }
112 }
113 add_action( 'pre_get_posts', 'sight_portfolio_pre_get_posts', 1 );
114
115 /**
116 * Filters the number of found posts for the query.
117 *
118 * @param int $found_posts The number of posts found.
119 * @param object $wp_query WP Query.
120 */
121 function sight_portfolio_found_posts( $found_posts, $wp_query ) {
122
123 if ( isset( $wp_query->query['is_sight_query'] ) ) {
124
125 $offset = isset( $wp_query->query['offset'] ) ? $wp_query->query['offset'] : 0;
126
127 $found_posts = (int) $found_posts - (int) $offset;
128 }
129
130 return $found_posts;
131 }
132 add_filter( 'found_posts', 'sight_portfolio_found_posts', 1, 2 );
133
134 /**
135 * Get More Posts
136 */
137 function sight_portfolio_load_more_posts() {
138
139 $posts_end = false;
140
141 // Response default.
142 $response = array(
143 'page' => 2,
144 'posts_per_page' => 10,
145 'query_data' => array(),
146 );
147
148 if ( wp_doing_ajax() ) {
149 check_ajax_referer();
150 }
151
152 // Set response values of ajax query.
153 if ( isset( $_POST['page'] ) && $_POST['page'] ) { // Input var ok.
154 $response['page'] = sanitize_key( $_POST['page'] ); // Input var ok; sanitization ok.
155 }
156 if ( isset( $_POST['posts_per_page'] ) && $_POST['posts_per_page'] ) { // Input var ok.
157 $response['posts_per_page'] = sanitize_key( $_POST['posts_per_page'] ); // Input var ok; sanitization ok.
158 }
159 if ( isset( $_POST['query_data'] ) && $_POST['query_data'] ) { // Input var ok.
160 $response['query_data'] = map_deep( json_decode( stripslashes( $_POST['query_data'] ), true ), 'sanitize_text_field' ); // Input var ok; sanitization ok.
161 }
162 if ( isset( $_POST['attributes'] ) && $_POST['attributes'] ) { // Input var ok.
163 $response['attributes'] = map_deep( json_decode( stripslashes( $_POST['attributes'] ), true ), 'sanitize_text_field' ); // Input var ok; sanitization ok.
164 }
165 if ( isset( $_POST['options'] ) && $_POST['options'] ) { // Input var ok.
166 $response['options'] = map_deep( json_decode( stripslashes( $_POST['options'] ), true ), 'sanitize_text_field' ); // Input var ok; sanitization ok.
167 }
168
169 // Set Query Vars.
170 $query_vars = array_merge(
171 (array) $response['query_data']['query_vars'],
172 array(
173 'is_sight_query' => true,
174 'paged' => (int) $response['page'],
175 'posts_per_page' => (int) $response['posts_per_page'],
176 )
177 );
178
179 // Supportfolio filtering for wp authors.
180 if ( $response['query_data']['is_author'] && $query_vars['author'] ) {
181 $query_vars['supportfolio_filters'] = true;
182 }
183
184 $attributes = $response['attributes'];
185 $options = $response['options'];
186
187 // Get Posts.
188 ob_start();
189
190 if ( isset( $_POST['terms'] ) && $_POST['terms'] ) { // Input var ok.
191 $terms = array_map( 'sanitize_text_field', $_POST['terms'] ); // Input var ok; sanitization ok.
192
193 if ( $terms ) {
194 $query_vars['tax_query'] = array();
195
196 $query_vars['tax_query'][] = array(
197 'taxonomy' => 'sight-categories',
198 'field' => 'slug',
199 'terms' => $terms,
200 );
201
202 $query_vars['tax_query']['relation'] = 'AND';
203 }
204 }
205
206 $the_query = new WP_Query( $query_vars );
207
208 $global_name = 'wp_query';
209
210 $GLOBALS[ $global_name ] = $the_query;
211
212 sight_portfolio_load_more_query_data( 'init', $response['query_data'] );
213
214 if ( $the_query->have_posts() ) :
215
216 // Set query vars, so that we can get them across all templates.
217 set_query_var( 'sight_query', $response['query_data'] );
218
219 // Get total number of posts.
220 $total = $the_query->post_count;
221
222 while ( $the_query->have_posts() ) :
223 $the_query->the_post();
224
225 // Start counting posts.
226 $current = $the_query->current_post + 1 + $query_vars['posts_per_page'] * $query_vars['paged'] - $query_vars['posts_per_page'];
227
228 // Check End of posts.
229 if ( $the_query->found_posts - $current <= 0 ) {
230 $posts_end = true;
231 }
232
233 $portfolio_entry = new Sight_Entry( $attributes, $options );
234
235 // Init portfolio entry.
236 $portfolio_entry->init();
237
238 // Get item project.
239 require apply_filters( 'sight_portfolio_item_path', SIGHT_PATH . 'render/handler/portfolio-entry.php', $attributes, $options, $portfolio_entry );
240
241 endwhile;
242
243 endif;
244
245 wp_reset_postdata();
246
247 $content = ob_get_clean();
248
249 if ( ! $content ) {
250 $posts_end = true;
251 }
252
253 // Return Result.
254 $result = array(
255 'posts_end' => $posts_end,
256 'content' => $content,
257 );
258
259 return $result;
260 }
261
262 /**
263 * AJAX Load More
264 */
265 function sight_portfolio_ajax_load_more() {
266
267 // Check Nonce.
268 check_ajax_referer();
269
270 // Get Posts.
271 $data = sight_portfolio_load_more_posts();
272
273 // Return Result.
274 wp_send_json_success( $data );
275
276 }
277 add_action( 'wp_ajax_sight_portfolio_ajax_load_more', 'sight_portfolio_ajax_load_more' );
278 add_action( 'wp_ajax_nopriv_sight_portfolio_ajax_load_more', 'sight_portfolio_ajax_load_more' );
279
280
281 /**
282 * More Posts API Response
283 *
284 * @param array $request REST API Request.
285 */
286 function sight_portfolio_more_posts_restapi( $request ) {
287
288 // Get Data.
289 $data = array(
290 'success' => true,
291 'data' => sight_portfolio_load_more_posts(),
292 );
293
294 // Return Result.
295 return rest_ensure_response( $data );
296 }
297
298 /**
299 * Register REST More Posts Routes
300 */
301 function sight_portfolio_register_more_posts_route() {
302
303 register_rest_route(
304 'sight/v1',
305 '/portfolio-more-posts',
306 array(
307 'methods' => WP_REST_Server::CREATABLE,
308 'callback' => 'sight_portfolio_more_posts_restapi',
309 'permission_callback' => function() {
310 return true;
311 },
312 )
313 );
314 }
315 add_action( 'rest_api_init', 'sight_portfolio_register_more_posts_route' );
316