· 6 years ago · Feb 12, 2020, 07:18 PM
1<?php
2
3defined( 'ABSPATH' ) || exit;
4
5if ( ! class_exists( 'Gebeco_Addons_Reservations' ) ) {
6
7 class Gebeco_Addons_Reservations {
8
9 public function __construct() {
10 // Add page options.
11 add_filter( 'gebeco_addons_options_sub_pages', array( $this, 'add_page_options' ) );
12 // Add option page items.
13 add_action( 'init', array( $this, 'add_options_page_items' ) );
14 // Register the rest routes.
15 add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
16 }
17
18 /**
19 * Filter the page options and add this module sub page.
20 *
21 * @param $options_sub_pages
22 *
23 * @return mixed
24 */
25 public function add_page_options( $options_sub_pages ) {
26 $options_sub_pages['reservations'] = array(
27 'page_title' => __( 'Vormerktermine', 'gebeco-addons' ),
28 'menu_slug' => 'gbc-reservations',
29 'parent_slug' => 'gbc-settings',
30 'post_id' => 'gbc-reservations',
31 );
32
33 return $options_sub_pages;
34 }
35
36 /**
37 * Add items to the page option.
38 */
39 public function add_options_page_items() {
40 // Check for function first.
41 if ( ! function_exists( 'acf_add_local_field_group' ) ) {
42 exit;
43 }
44
45 // Add the items.
46 acf_add_local_field_group( array(
47 'key' => 'reservations',
48 'title' => __( 'Vormerktermine', 'gebeco-addons' ),
49 'fields' => array(
50 array(
51 'key' => 'confirmation_page',
52 'label' => __( 'Bestätigungsseite auswählen', 'gebeco-addons' ),
53 'name' => 'confirmation_page',
54 'type' => 'post_object',
55 'instructions' => '',
56 'required' => 0,
57 'conditional_logic' => 0,
58 'wrapper' => array(
59 'width' => '',
60 'class' => '',
61 'id' => '',
62 ),
63 'post_type' => array(
64 0 => 'page',
65 ),
66 'taxonomy' => array(),
67 'allow_null' => 1,
68 'multiple' => 0,
69 'return_format' => 'id',
70 'ui' => 1,
71 ),
72 array(
73 'key' => 'confirmation_page_gox',
74 'label' => __( 'Confirmation Page Gox', 'gebeco-addons' ),
75 'name' => 'confirmation_page_gox',
76 'type' => 'post_object',
77 'instructions' => '',
78 'required' => 0,
79 'conditional_logic' => 0,
80 'wrapper' => array(
81 'width' => '',
82 'class' => '',
83 'id' => '',
84 ),
85 'post_type' => array(
86 0 => 'page',
87 ),
88 'taxonomy' => array(),
89 'allow_null' => 1,
90 'multiple' => 0,
91 'return_format' => 'id',
92 'ui' => 1,
93 ),
94 ),
95 'location' => array(
96 array(
97 array(
98 'param' => 'options_page',
99 'operator' => '==',
100 'value' => 'gbc-reservations',
101 ),
102 ),
103 ),
104 'menu_order' => 0,
105 'position' => 'normal',
106 'style' => 'seamless',
107 'label_placement' => 'left',
108 'instruction_placement' => 'label',
109 'hide_on_screen' => '',
110 'active' => 1,
111 'description' => '',
112 ) );
113 }
114
115 /**
116 * Register REST API routes.
117 */
118 public function register_rest_routes() {
119 gbc_register_rest_routes( '/reservations/confirmation-page', array(
120 'methods' => 'GET',
121 'callback' => array( $this, 'get_confirmation_page' ),
122 ), true );
123 }
124
125 /**
126 * @param $request WP_REST_Request
127 *
128 * @return array|WP_Error|WP_Post|WP_REST_Response
129 */
130 public function get_confirmation_page( $request ) {
131 if( $request->get_param('type' ) === 'gox' ) {
132 $confirmation_page_id = gbc_acf_get_field( 'confirmation_page_gox', null, 'gbc-reservations' );
133 } else {
134 $confirmation_page_id = gbc_acf_get_field( 'confirmation_page', null, 'gbc-reservations' );
135 }
136
137 // If not defined, return an error
138 if ( ! $confirmation_page_id ) {
139 return new WP_REST_Response( array( 'message' => __( 'Page does not exist', 'gebeco-addons' ) ), 404 );
140 }
141
142 // Post controller
143 $post_controller = new WP_REST_Posts_Controller( 'page' );
144
145 $post = $this->get_post( $confirmation_page_id );
146
147 if ( is_wp_error( $post ) ) {
148 return $post;
149 }
150
151 $data = $post_controller->prepare_item_for_response( $post, $request );
152 $response = rest_ensure_response( $data );
153
154 if ( is_post_type_viewable( get_post_type_object( $post->post_type ) ) ) {
155 $response->link_header( 'alternate', get_permalink( $post->ID ), array( 'type' => 'text/html' ) );
156 }
157
158 return array( $response );
159 }
160
161 /**
162 * Get the post, if the ID is valid.
163 *
164 * @since 4.7.2
165 *
166 * @param int $id Supplied ID.
167 *
168 * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
169 */
170 protected function get_post( $id ) {
171 $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
172 if ( (int) $id <= 0 ) {
173 return $error;
174 }
175
176 $post = get_post( (int) $id );
177 if ( empty( $post ) || empty( $post->ID ) ) {
178 return $error;
179 }
180
181 return $post;
182 }
183 }
184
185}