| 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 |
* Get More Posts |
| 94 |
*/ |
| 95 |
function sight_portfolio_load_more_posts() { |
| 96 |
|
| 97 |
$posts_end = false; |
| 98 |
|
| 99 |
// Response default. |
| 100 |
$response = array( |
| 101 |
'page' => 2, |
| 102 |
'posts_per_page' => 10, |
| 103 |
'query_data' => array(), |
| 104 |
); |
| 105 |
|
| 106 |
if ( wp_doing_ajax() ) { |
| 107 |
check_ajax_referer(); |
| 108 |
} |
| 109 |
|
| 110 |
// Set response values of ajax query. |
| 111 |
if ( isset( $_POST['page'] ) && $_POST['page'] ) { // Input var ok. |
| 112 |
$response['page'] = sanitize_key( $_POST['page'] ); // Input var ok; sanitization ok. |
| 113 |
} |
| 114 |
if ( isset( $_POST['posts_per_page'] ) && $_POST['posts_per_page'] ) { // Input var ok. |
| 115 |
$response['posts_per_page'] = sanitize_key( $_POST['posts_per_page'] ); // Input var ok; sanitization ok. |
| 116 |
} |
| 117 |
if ( isset( $_POST['query_data'] ) && $_POST['query_data'] ) { // Input var ok. |
| 118 |
$response['query_data'] = map_deep( json_decode( stripslashes( $_POST['query_data'] ), true ), 'sanitize_text_field' ); // Input var ok; sanitization ok. |
| 119 |
} |
| 120 |
if ( isset( $_POST['attributes'] ) && $_POST['attributes'] ) { // Input var ok. |
| 121 |
$response['attributes'] = map_deep( json_decode( stripslashes( $_POST['attributes'] ), true ), 'sanitize_text_field' ); // Input var ok; sanitization ok. |
| 122 |
} |
| 123 |
if ( isset( $_POST['options'] ) && $_POST['options'] ) { // Input var ok. |
| 124 |
$response['options'] = map_deep( json_decode( stripslashes( $_POST['options'] ), true ), 'sanitize_text_field' ); // Input var ok; sanitization ok. |
| 125 |
} |
| 126 |
|
| 127 |
// Set Query Vars. |
| 128 |
$query_vars = array_merge( |
| 129 |
(array) $response['query_data']['query_vars'], |
| 130 |
array( |
| 131 |
'is_post_query' => true, |
| 132 |
'paged' => (int) $response['page'], |
| 133 |
'posts_per_page' => (int) $response['posts_per_page'], |
| 134 |
) |
| 135 |
); |
| 136 |
|
| 137 |
// Supportfolio filtering for wp authors. |
| 138 |
if ( $response['query_data']['is_author'] && $query_vars['author'] ) { |
| 139 |
$query_vars['supportfolio_filters'] = true; |
| 140 |
} |
| 141 |
|
| 142 |
$attributes = $response['attributes']; |
| 143 |
$options = $response['options']; |
| 144 |
|
| 145 |
// Get Posts. |
| 146 |
ob_start(); |
| 147 |
|
| 148 |
if ( isset( $_POST['terms'] ) && $_POST['terms'] ) { // Input var ok. |
| 149 |
$terms = array_map( 'sanitize_text_field', $_POST['terms'] ); // Input var ok; sanitization ok. |
| 150 |
|
| 151 |
if ( $terms ) { |
| 152 |
$query_vars['tax_query'] = array(); |
| 153 |
|
| 154 |
$query_vars['tax_query'][] = array( |
| 155 |
'taxonomy' => 'sight-categories', |
| 156 |
'field' => 'slug', |
| 157 |
'terms' => $terms, |
| 158 |
); |
| 159 |
|
| 160 |
$query_vars['tax_query']['relation'] = 'AND'; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$the_query = new WP_Query( $query_vars ); |
| 165 |
|
| 166 |
$global_name = 'wp_query'; |
| 167 |
|
| 168 |
$GLOBALS[ $global_name ] = $the_query; |
| 169 |
|
| 170 |
sight_portfolio_load_more_query_data( 'init', $response['query_data'] ); |
| 171 |
|
| 172 |
if ( $the_query->have_posts() ) : |
| 173 |
|
| 174 |
// Set query vars, so that we can get them across all templates. |
| 175 |
set_query_var( 'sight_query', $response['query_data'] ); |
| 176 |
|
| 177 |
// Get total number of posts. |
| 178 |
$total = $the_query->post_count; |
| 179 |
|
| 180 |
while ( $the_query->have_posts() ) : |
| 181 |
$the_query->the_post(); |
| 182 |
|
| 183 |
// Start counting posts. |
| 184 |
$current = $the_query->current_post + 1 + $query_vars['posts_per_page'] * $query_vars['paged'] - $query_vars['posts_per_page']; |
| 185 |
|
| 186 |
// Check End of posts. |
| 187 |
if ( $the_query->found_posts - $current <= 0 ) { |
| 188 |
$posts_end = true; |
| 189 |
} |
| 190 |
|
| 191 |
$portfolio_entry = new Sight_Entry( $attributes, $options ); |
| 192 |
|
| 193 |
// Init portfolio entry. |
| 194 |
$portfolio_entry->init(); |
| 195 |
|
| 196 |
// Get item project. |
| 197 |
require apply_filters( 'sight_portfolio_item_path', SIGHT_PATH . 'render/handler/portfolio-entry.php', $attributes, $options, $portfolio_entry ); |
| 198 |
|
| 199 |
endwhile; |
| 200 |
|
| 201 |
endif; |
| 202 |
|
| 203 |
wp_reset_postdata(); |
| 204 |
|
| 205 |
$content = ob_get_clean(); |
| 206 |
|
| 207 |
if ( ! $content ) { |
| 208 |
$posts_end = true; |
| 209 |
} |
| 210 |
|
| 211 |
// Return Result. |
| 212 |
$result = array( |
| 213 |
'posts_end' => $posts_end, |
| 214 |
'content' => $content, |
| 215 |
); |
| 216 |
|
| 217 |
return $result; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* AJAX Load More |
| 222 |
*/ |
| 223 |
function sight_portfolio_ajax_load_more() { |
| 224 |
|
| 225 |
// Check Nonce. |
| 226 |
check_ajax_referer(); |
| 227 |
|
| 228 |
// Get Posts. |
| 229 |
$data = sight_portfolio_load_more_posts(); |
| 230 |
|
| 231 |
// Return Result. |
| 232 |
wp_send_json_success( $data ); |
| 233 |
|
| 234 |
} |
| 235 |
add_action( 'wp_ajax_sight_portfolio_ajax_load_more', 'sight_portfolio_ajax_load_more' ); |
| 236 |
add_action( 'wp_ajax_nopriv_sight_portfolio_ajax_load_more', 'sight_portfolio_ajax_load_more' ); |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* More Posts API Response |
| 241 |
* |
| 242 |
* @param array $request REST API Request. |
| 243 |
*/ |
| 244 |
function sight_portfolio_more_posts_restapi( $request ) { |
| 245 |
|
| 246 |
// Get Data. |
| 247 |
$data = array( |
| 248 |
'success' => true, |
| 249 |
'data' => sight_portfolio_load_more_posts(), |
| 250 |
); |
| 251 |
|
| 252 |
// Return Result. |
| 253 |
return rest_ensure_response( $data ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Register REST More Posts Routes |
| 258 |
*/ |
| 259 |
function sight_portfolio_register_more_posts_route() { |
| 260 |
|
| 261 |
register_rest_route( |
| 262 |
'sight/v1', |
| 263 |
'/portfolio-more-posts', |
| 264 |
array( |
| 265 |
'methods' => WP_REST_Server::CREATABLE, |
| 266 |
'callback' => 'sight_portfolio_more_posts_restapi', |
| 267 |
'permission_callback' => function() { |
| 268 |
return true; |
| 269 |
}, |
| 270 |
) |
| 271 |
); |
| 272 |
} |
| 273 |
add_action( 'rest_api_init', 'sight_portfolio_register_more_posts_route' ); |
| 274 |
|