| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\SiteNavigation\Data\Endpoints; |
| 4 |
|
| 5 |
use Elementor\Core\Base\Document; |
| 6 |
use Elementor\Core\Kits\Documents\Kit; |
| 7 |
use Elementor\Data\V2\Base\Endpoint; |
| 8 |
use Elementor\Plugin; |
| 9 |
use Elementor\TemplateLibrary\Source_Local; |
| 10 |
use Elementor\Utils; |
| 11 |
use WP_REST_Server; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
class Recent_Posts extends Endpoint { |
| 18 |
|
| 19 |
public function register_items_route( $methods = WP_REST_Server::READABLE, $args = [] ) { |
| 20 |
$args = [ |
| 21 |
'posts_per_page' => [ |
| 22 |
'description' => 'Number of posts to return', |
| 23 |
'type' => 'integer', |
| 24 |
'required' => true, |
| 25 |
'sanitize_callback' => 'absint', |
| 26 |
'validate_callback' => 'rest_validate_request_arg', |
| 27 |
], |
| 28 |
'post_type' => [ |
| 29 |
'description' => 'Post types to retrieve', |
| 30 |
'type' => 'array', |
| 31 |
'required' => false, |
| 32 |
'default' => [ 'page', 'post', Source_Local::CPT ], |
| 33 |
'sanitize_callback' => 'rest_sanitize_array', |
| 34 |
'validate_callback' => 'rest_validate_request_arg', |
| 35 |
], |
| 36 |
'post__not_in' => [ |
| 37 |
'description' => 'Post id`s to exclude', |
| 38 |
'type' => 'array', |
| 39 |
'required' => [], |
| 40 |
'sanitize_callback' => 'wp_parse_id_list', |
| 41 |
'validate_callback' => 'rest_validate_request_arg', |
| 42 |
], |
| 43 |
]; |
| 44 |
|
| 45 |
parent::register_items_route( $methods, $args ); |
| 46 |
} |
| 47 |
|
| 48 |
public function get_name() { |
| 49 |
return 'recent-posts'; |
| 50 |
} |
| 51 |
|
| 52 |
public function get_format() { |
| 53 |
return 'site-navigation/recent-posts'; |
| 54 |
} |
| 55 |
|
| 56 |
public function get_items( $request ) { |
| 57 |
$args = [ |
| 58 |
'posts_per_page' => $request->get_param( 'posts_per_page' ), |
| 59 |
'post_type' => $request->get_param( 'post_type' ), |
| 60 |
'fields' => 'ids', |
| 61 |
'meta_query' => [ |
| 62 |
[ |
| 63 |
'key' => Document::TYPE_META_KEY, |
| 64 |
'value' => Kit::get_type(), // Exclude kits. |
| 65 |
'compare' => '!=', |
| 66 |
], |
| 67 |
], |
| 68 |
]; |
| 69 |
|
| 70 |
$exclude = $request->get_param( 'post__not_in' ); |
| 71 |
|
| 72 |
if ( ! empty( $exclude ) ) { |
| 73 |
$args['post__not_in'] = $exclude; |
| 74 |
} |
| 75 |
|
| 76 |
$recently_edited_query = Utils::get_recently_edited_posts_query( $args ); |
| 77 |
|
| 78 |
$recent = []; |
| 79 |
|
| 80 |
foreach ( $recently_edited_query->posts as $id ) { |
| 81 |
$document = Plugin::$instance->documents->get( $id ); |
| 82 |
|
| 83 |
$recent[] = [ |
| 84 |
'id' => $id, |
| 85 |
'title' => get_the_title( $id ), |
| 86 |
'edit_url' => $document->get_edit_url(), |
| 87 |
'date_modified' => get_post_timestamp( $id, 'modified' ), |
| 88 |
'type' => [ |
| 89 |
'post_type' => get_post_type( $id ), |
| 90 |
'doc_type' => $document->get_name(), |
| 91 |
'label' => $document->get_title(), |
| 92 |
], |
| 93 |
'user_can' => [ |
| 94 |
'edit' => current_user_can( 'edit_post', $id ), |
| 95 |
], |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
return $recent; |
| 100 |
} |
| 101 |
} |
| 102 |
|