Export.php
2 years ago
GetFields.php
4 months ago
Import.php
2 years ago
Options.php
1 week ago
Pages.php
2 years ago
Posts.php
4 months ago
Services.php
1 year ago
Pages.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Pages. |
| 4 | * |
| 5 | * @package ShopPress |
| 6 | */ |
| 7 | |
| 8 | namespace ShopPress\Admin\API; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use ShopPress\Admin; |
| 13 | |
| 14 | class Pages { |
| 15 | /** |
| 16 | * Instance of this class. |
| 17 | * |
| 18 | * @since 1.2.0 |
| 19 | */ |
| 20 | public static $instance; |
| 21 | |
| 22 | /** |
| 23 | * Provides access to a single instance of a module using the singleton pattern. |
| 24 | * |
| 25 | * @since 1.2.0 |
| 26 | * |
| 27 | * @return object |
| 28 | */ |
| 29 | public static function instance() { |
| 30 | if ( self::$instance === null ) { |
| 31 | self::$instance = new self(); |
| 32 | } |
| 33 | return self::$instance; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Add a page. |
| 38 | * |
| 39 | * @since 1.2.0 |
| 40 | * |
| 41 | * @param object $request |
| 42 | */ |
| 43 | public function add_page( $request ) { |
| 44 | $post_type = $request->get_param( 'type' ); |
| 45 | $component = $request->get_param( 'component' ); |
| 46 | $name = $request->get_param( 'name' ); |
| 47 | $builder = $request->get_param( 'builder' ); |
| 48 | |
| 49 | $custom_type_meta = sanitize_text_field( $component ); |
| 50 | $builder_meta = sanitize_text_field( $builder ); |
| 51 | $page_id = \ShopPress\Templates\Main::add_post( $post_type, $name ); |
| 52 | $page_array = array(); |
| 53 | |
| 54 | if ( $page_id ) { |
| 55 | |
| 56 | update_post_meta( $page_id, 'custom_type', $custom_type_meta ); |
| 57 | update_post_meta( $page_id, 'sp_builder_type', $builder_meta ); |
| 58 | |
| 59 | $page = get_post( $page_id ); |
| 60 | |
| 61 | $page_array = array( |
| 62 | 'page_id' => $page->ID, |
| 63 | 'page_title' => $page->post_title, |
| 64 | 'builder_type' => sp_get_builder_type( $page->ID ), |
| 65 | ); |
| 66 | |
| 67 | return new \WP_REST_Response( |
| 68 | $page_array, |
| 69 | 200 |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return $page_array; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Delete a page. |
| 78 | * |
| 79 | * @since 1.2.0 |
| 80 | * |
| 81 | * @param object $request |
| 82 | */ |
| 83 | public function delete_page( $request ) { |
| 84 | $page_id = $request->get_param( 'ID' ); |
| 85 | |
| 86 | $deleted_page = wp_delete_post( $page_id, false ); |
| 87 | |
| 88 | return new \WP_REST_Response( |
| 89 | $deleted_page->ID, |
| 90 | 200 |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Prepare pages data. |
| 96 | * |
| 97 | * @since 1.2.0 |
| 98 | * |
| 99 | * @param object $pages |
| 100 | */ |
| 101 | private function prepare_pages_data( $pages ) { |
| 102 | |
| 103 | if ( ! is_array( $pages ) || empty( $pages ) ) { |
| 104 | return array(); |
| 105 | } |
| 106 | |
| 107 | $prepare_pages_data = array(); |
| 108 | |
| 109 | foreach ( $pages as $page ) { |
| 110 | |
| 111 | $page_array = array( |
| 112 | 'page_id' => $page->ID, |
| 113 | 'page_title' => $page->post_title, |
| 114 | 'builder_type' => sp_get_builder_type( $page->ID ), |
| 115 | ); |
| 116 | |
| 117 | $prepare_pages_data[] = $page_array; |
| 118 | } |
| 119 | |
| 120 | return $prepare_pages_data; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get pages. |
| 125 | * |
| 126 | * @since 1.2.0 |
| 127 | * |
| 128 | * @param object $request |
| 129 | */ |
| 130 | public function get_pages( $request ) { |
| 131 | $post_type = $request->get_param( 'type' ); |
| 132 | $component = $request->get_param( 'component' ); |
| 133 | |
| 134 | if ( ! $post_type || ! $component ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | $args = array( |
| 139 | 'post_type' => $post_type, |
| 140 | 'posts_per_page' => 99, |
| 141 | ); |
| 142 | |
| 143 | if ( 'shoppress_loop' !== $post_type ) { |
| 144 | |
| 145 | $args['meta_query']['custom_type'] = array( |
| 146 | 'key' => 'custom_type', |
| 147 | 'value' => $component, |
| 148 | 'compare' => '=', |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | $pages = get_posts( $args ); |
| 153 | |
| 154 | if ( ! is_wp_error( $pages ) ) { |
| 155 | |
| 156 | $prepared_data = $this->prepare_pages_data( $pages ); |
| 157 | |
| 158 | return new \WP_REST_Response( |
| 159 | $prepared_data, |
| 160 | 200 |
| 161 | ); |
| 162 | } else { |
| 163 | |
| 164 | $error_message = $pages->get_error_message(); |
| 165 | |
| 166 | return new \WP_REST_Response( |
| 167 | $error_message, |
| 168 | 500 |
| 169 | ); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 |