PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-import / src / endpoints / class-menu-item.php

class-menu-item.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-import/src/endpoints/class-menu-item.php

136 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Menu items REST route
4 *
5 * @package automattic/jetpack-import
6 */
7
8 namespace Automattic\Jetpack\Import\Endpoints;
9
10 use WP_Error;
11 use WP_REST_Request;
12 use WP_REST_Response;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit( 0 );
16 }
17
18 /**
19 * Class Menu_Item
20 */
21 class Menu_Item extends \WP_REST_Menu_Items_Controller {
22
23 /**
24 * Base class
25 */
26 use Import;
27
28 /**
29 * The Import ID add a new item to the schema.
30 */
31 use Import_ID;
32
33 /**
34 * Whether the controller supports batching. Default true.
35 *
36 * @var array
37 */
38 protected $allow_batch = array( 'v1' => true );
39
40 /**
41 * Constructor.
42 */
43 public function __construct() {
44 parent::__construct( 'nav_menu_item' );
45
46 // @see add_term_meta
47 $this->import_id_meta_type = 'post';
48 }
49
50 /**
51 * Adds the schema from additional fields to a schema array.
52 *
53 * The type of object is inferred from the passed schema.
54 *
55 * @param array $schema Schema array.
56 * @return array Modified Schema array.
57 */
58 public function add_additional_fields_schema( $schema ) {
59 // WXR saves menu parent as slug, so we need to overwrite the schema.
60 $schema['properties']['menus']['description'] = __( 'The parent menu slug.', 'jetpack-import' );
61 $schema['properties']['menus']['type'] = 'string';
62
63 // Add the import unique ID to the schema.
64 return $this->add_unique_identifier_to_schema( $schema );
65 }
66
67 /**
68 * Creates a single menu item.
69 *
70 * @param WP_REST_Request $request Full details about the request.
71 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
72 */
73 public function create_item( $request ) {
74 // Set the WP_IMPORTING constant to prevent sync notifications
75 $this->set_importing();
76
77 if ( ! empty( $request['menus'] ) ) {
78 $menu_id = \term_exists( $request['menus'], 'nav_menu' );
79
80 if ( $menu_id ) {
81 // Overwrite the menu item parent menu ID.
82 $request['menus'] = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
83 }
84 }
85
86 if ( ! empty( $request['parent'] ) ) {
87 $query = $this->get_import_db_query( $request['parent'] );
88 $query['post_type'] = 'nav_menu_item';
89 $parent = \get_posts( $query );
90
91 // Overwrite the parent ID.
92 $request['parent'] = is_array( $parent ) && count( $parent ) ? $parent[0] : 0;
93 }
94
95 // A menu item can be a custom link or a post, page, category or attachment.
96 if ( ! empty( $request['object_id'] ) ) {
97 $id = null;
98
99 if ( $request['object'] === 'category' ) {
100 if ( $request['object_id'] === 1 ) {
101 // The default category is always ID 1, no need to search.
102 $id = 1;
103 } else {
104 $query = $this->get_import_db_query( $request['object_id'] );
105 $categories = \get_categories( $this->get_import_db_query( $request['object_id'] ) );
106
107 // Overwrite the category ID.
108 $id = is_array( $categories ) && count( $categories ) ? $categories[0] : null;
109 }
110 } elseif ( $request['object'] === 'page' ) {
111 $pages = \get_pages( $this->get_import_db_query( $request['object_id'] ) );
112
113 // Overwrite the page ID.
114 $id = is_array( $pages ) && count( $pages ) ? $pages[0]->ID : null;
115 } elseif ( $request['object'] === 'post' || $request['object'] === 'attachment' ) {
116 $posts = \get_posts( $this->get_import_db_query( $request['object_id'] ) );
117
118 // Overwrite the post ID.
119 $id = is_array( $posts ) && count( $posts ) ? $posts[0] : null;
120 }
121
122 if ( empty( $id ) ) {
123 // Not found the object or a custom menu item, remove the fields.
124 unset( $request['object_id'] );
125 unset( $request['object'] );
126 } else {
127 $request['object_id'] = $id;
128 }
129 }
130
131 $response = parent::create_item( $request );
132
133 return $this->add_import_id_metadata( $request, $response );
134 }
135 }
136