forked from idxbroker/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrappers.php
409 lines (383 loc) · 12.4 KB
/
wrappers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
namespace IDX;
/**
* Wrappers class.
*/
class Wrappers {
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
$this->idx_api = new Idx_Api();
add_action( 'init', array( $this, 'register_wrapper_post_type' ) );
add_action( 'admin_init', array( $this, 'manage_idx_wrapper_capabilities' ) );
add_filter( 'default_content', array( $this, 'idx_wrapper_content' ), 10, 2 );
add_action( 'wp_enqueue_scripts', array( $this, 'wrapper_styles' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'set_wrapper_page' ) );
}
/**
* Idx_api
*
* @var mixed
* @access public
*/
public $idx_api;
/**
* Register_wrapper_post_type function.
*
* @access public
* @return void
*/
public function register_wrapper_post_type() {
$labels = array(
'name' => 'Wrappers',
'singular_name' => 'Wrapper',
'add_new' => 'Add Wrapper',
'add_new_item' => 'Add New Wrapper',
'edit_item' => 'Edit Wrapper',
'new_item' => 'New Wrapper',
'view_item' => 'View Wrapper',
'search_items' => 'Search Wrappers',
'not_found' => 'No Wrappers found',
'not_found_in_trash' => 'No Wrappers found in Trash',
'parent_item_colon' => '',
'parent' => 'Parent Wrapper',
);
$capabilities = array(
'publish_posts' => 'publish_idx_wrappers',
'edit_posts' => 'edit_idx_wrappers',
'edit_others_posts' => 'edit_others_idx_wrappers',
'delete_posts' => 'delete_idx_wrappers',
'delete_others_posts' => 'delete_others_idx_wrappers',
'read_private_posts' => 'read_private_idx_wrappers',
'edit_post' => 'edit_idx_wrapper',
'delete_post' => 'delete_idx_wrapper',
'read_post' => 'read_idx_wrapper',
);
$args = array(
'public' => true,
'labels' => $labels,
'label' => 'Wrappers',
'description' => 'Custom Posts Created To Match IDX Pages to the Website',
'exclude_from_search' => true,
'show_in_menu' => 'idx-broker',
'show_in_nav_menus' => false,
'capability_type' => array( 'idx_wrapper', 'idx_wrappers' ),
'capabilities' => $capabilities,
'has_archive' => false,
'hierarchical' => false,
'rewrite' => array( 'pages' => false ),
'supports' => array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'revisions',
'equity-layouts',
'equity-cpt-archives-settings',
'genesis-seo',
'genesis-layouts',
'genesis-simple-sidebars',
'genesis-cpt-archives-settings',
'publicize',
'wpcom-markdown',
),
);
register_post_type( 'idx-wrapper', $args );
}
/**
* Wrapper_styles function.
*
* @access public
* @return void
*/
public function wrapper_styles() {
// Add styles hiding the post title and previous/next links via the stylesheet.
global $post;
if ( $post && 'idx-wrapper' === $post->post_type ) {
wp_enqueue_style( 'idx-wrappers', IMPRESS_IDX_URL . 'assets/css/idx-wrappers.min.css', [], '1.0.0' );
}
}
/**
* Manage_idx_wrapper_capabilities function.
*
* @access public
* @return void
*/
public function manage_idx_wrapper_capabilities() {
// gets the role to add capabilities to.
if ( current_user_can( 'edit_others_posts' ) ) {
$current_user = wp_get_current_user();
// replicate all the remapped capabilites from the custom post type.
$caps = array(
'publish_idx_wrappers',
'edit_idx_wrappers',
'edit_others_idx_wrappers',
'delete_idx_wrappers',
'delete_others_idx_wrappers',
'read_private_idx_wrappers',
'edit_idx_wrapper',
'delete_idx_wrapper',
'read_idx_wrapper',
);
// give all the capabilities to the administrator.
foreach ( $caps as $cap ) {
$current_user->add_cap( $cap );
}
}
}
/**
* Check if theme includes idxstart and stop tags
*/
public function does_theme_include_idx_tag() {
// default page content
// the empty div is for any content they add to the visual editor so it displays.
$post_content = '<div></div><div id="idxStart" style="display: none;"></div><div id="idxStop" style="display: none;"></div>';
// get theme to check start/stop tag.
$does_theme_include_idx_tag = false;
$template_root = get_theme_root() . DIRECTORY_SEPARATOR . get_stylesheet();
$files = scandir( $template_root );
foreach ( $files as $file ) {
$path = $template_root . DIRECTORY_SEPARATOR . $file;
if ( is_file( $path ) && preg_match( '/.*\.php/', $file ) ) {
$content = file_get_contents( $template_root . DIRECTORY_SEPARATOR . $file );
if ( preg_match( '/<div[^>\n]+?id=[\'"]idxstart[\'"].*?(\/>|><\/div>)/i', $content ) ) {
if ( preg_match( '/<div[^>\n]+?id=[\'"]idxstop[\'"].*?(\/>|><\/div>)/i', $content ) ) {
$does_theme_include_idx_tag = true;
break;
}
}
}
}
if ( $does_theme_include_idx_tag || function_exists( 'equity' ) ) {
$post_content = '';
}
return $post_content;
}
/**
* Idx_wrapper_content function.
*
* @access public
* @param mixed $content - Content.
* @param mixed $post - Post.
* @return mixed
*/
public function idx_wrapper_content( $content, $post ) {
if ( 'idx-wrapper' === $post->post_type ) {
$content = $this->does_theme_include_idx_tag();
return $content;
}
}
/**
* Idx_create_dynamic_page function.
*
* @param string $title Wrapper page title.
* @access public
* @return WP_Error|null
*/
public function idx_create_dynamic_page( $title ) {
// Default page content.
$post_content = $this->does_theme_include_idx_tag();
$submitted_title = sanitize_text_field( wp_unslash( $title ) );
$post_title = $submitted_title ? $submitted_title : 'Properties';
// Check if already exists.
$existing_post = get_page_by_title( $post_title, OBJECT, 'idx-wrapper' );
if ( $existing_post ) {
return null;
}
$new_post = [
'post_title' => $post_title,
'post_name' => $post_title,
'post_content' => $post_content,
'post_type' => 'idx-wrapper',
'post_status' => 'publish',
];
$wrapper_page_id = wp_insert_post( $new_post );
update_option( 'idx_broker_dynamic_wrapper_page_name', $post_title, false );
update_option( 'idx_broker_dynamic_wrapper_page_id', $wrapper_page_id, false );
$wrapper_page_url = get_permalink( $wrapper_page_id );
$error = $this->idx_api->set_wrapper( 'global', $wrapper_page_url );
update_post_meta( $wrapper_page_id, 'idx-wrapper-page', 'global' );
if ( is_wp_error( $error ) ) {
return $error;
}
return null;
}
/**
* Idx_ajax_delete_dynamic_page function.
*
* @access public
* @return void
*/
public function idx_ajax_delete_dynamic_page() {
// User capability check.
if ( ! current_user_can( 'delete_others_posts' ) ) {
wp_die();
}
// Validate and process request.
if ( isset( $_POST['wrapper_page_id'], $_POST['nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'idx-settings-wrapper-delete-nonce' ) ) {
$wrapper_page_id = sanitize_text_field( wp_unslash( $_POST['wrapper_page_id'] ) );
// Verify retrieved post type is idx-wrapper before deleting.
if ( get_post_type( $wrapper_page_id ) === 'idx-wrapper' ) {
wp_delete_post( $wrapper_page_id, true );
wp_trash_post( $wrapper_page_id );
}
}
wp_die();
}
/**
* Dynamic wrapper requires the pageID, not the UID, so we must strip out the account number from the UID.
*
* @param string $uid - UID.
* @return string
*/
public function convert_uid_to_id( $uid ) {
return substr( $uid, strpos( $uid, '-' ) + 1 );
}
/**
* Is_selected function.
*
* @access public
* @param mixed $value - Selected value.
* @return mixed
*/
public function is_selected( $value ) {
$post_id = get_the_ID();
$saved_wrapper_page = get_post_meta( $post_id, 'idx-wrapper-page', true );
if ( ! empty( $saved_wrapper_page ) ) {
$saved_wrapper_page = get_post_meta( $post_id, 'idx-wrapper-page', true );
}
if ( ! empty( $value ) && $value === $saved_wrapper_page ) {
return 'selected';
}
}
/**
* Wrapper_page_dropdown function.
*
* @access public
* @param mixed $system_links - System Links.
* @param mixed $saved_links - Saved Links.
* @return void
*/
public function wrapper_page_dropdown( $system_links, $saved_links ) {
echo '<select class="idx-wrapper-page" name="idx-wrapper-page" style="width: 100%;">';
echo '<option value="none"' . esc_attr( $this->is_selected( 'none' ) ) . '>None</option>';
echo '<option value="global"' . esc_attr( $this->is_selected( 'global' ) ) . '>Globally</option>';
foreach ( $system_links as $system_link ) {
$uid = $system_link->uid;
$name = $system_link->name;
$id = $this->convert_uid_to_id( $uid );
echo '<option value="' . esc_attr( $id ) . '" ' . esc_attr( $this->is_selected( $id ) ) . '>' . esc_html( $name ) . '</option>';
}
if ( $this->idx_api->platinum_account_type() ) {
foreach ( $saved_links as $saved_link ) {
$id = $saved_link->id;
$name = $saved_link->linkTitle;
echo '<option value="' . esc_attr( $id ) . '" ' . esc_attr( $this->is_selected( $id ) ) . '>' . esc_html( $name ) . '</option>';
}
}
echo '</select>';
}
/**
* Wrapper_page_ui function.
*
* @access public
* @return void
*/
public function wrapper_page_ui() {
// add metabox interface when editing a wrapper page (with none and global options)
// This UI should display the current page set
// when saving a post, save the meta of which page is set.
$system_links = $this->idx_api->idx_api_get_systemlinks();
$saved_links = $this->idx_api->idx_api_get_savedlinks();
wp_nonce_field( 'idx-wrapper-page', 'idx-wrapper-page-nonce' );
$this->wrapper_page_dropdown( $system_links, $saved_links );
wp_enqueue_style( 'select2' );
wp_enqueue_script( 'select2' );
wp_enqueue_script( 'idx-wrapper', plugins_url( '../assets/js/idx-wrappers.min.js', __FILE__ ), [], '1.0', false );
}
/**
* Add_meta_box function.
*
* @access public
* @param mixed $post_type - Post type.
* @return void
*/
public function add_meta_box( $post_type ) {
$post_types = array( 'idx-wrapper' ); // limit meta box to certain post types.
if ( in_array( $post_type, $post_types ) ) {
add_meta_box(
'set_wrapper_page',
'Apply Wrapper to IDX Pages',
array( $this, 'wrapper_page_ui' ),
$post_type,
'side',
'low'
);
}
}
/**
* Set_wrapper_page function.
*
* @access public
* @param mixed $post_id - Post ID.
* @return mixed
*/
public function set_wrapper_page( $post_id ) {
// saved idx page ID.
if ( empty( $_POST ) || ! current_user_can( 'publish_posts' ) || ! current_user_can( 'edit_posts' ) ) {
return;
}
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || empty( $_POST['idx-wrapper-page-nonce'] ) ) {
return $post_id;
}
// Validate and process request.
if ( isset( $_POST['idx-wrapper-page-nonce'], $_POST['idx-wrapper-page'] ) || wp_verify_nonce( sanitize_key( $_POST['idx-wrapper-page-nonce'] ), 'idx-wrapper-page' ) ) {
// meta_value is the IDX Broker page ID (or 'none' or 'global') that should have its wrapper updated
$meta_value = sanitize_text_field( wp_unslash( $_POST['idx-wrapper-page'] ) );
// If 'none' was selected on the post, don't update anything
if ( 'none' === $meta_value ) {
return;
} elseif ( 'global' === $meta_value ) {
$this->idx_api->set_wrapper( $idx_page_id, '' );
}
// Get the edited wrapper's URL to set the wrapper with
$wrapper_page_url = get_permalink();
$this->idx_api->set_wrapper( $meta_value, $wrapper_page_url );
update_post_meta( $post_id, 'idx-wrapper-page', $meta_value );
}
}
/**
* Find_idx_url function.
*
* @access public
* @param mixed $post_id - Post ID.
* @return mixed
*/
public function find_idx_url( $post_id ) {
$post = get_post( $post_id );
$url = $post->post_name;
$system_links = $this->idx_api->idx_api_get_systemlinks();
$saved_links = $this->idx_api->idx_api_get_savedlinks();
foreach ( $system_links as $link ) {
if ( $link->url === $url ) {
$uid = $link->uid;
$name = $link->name;
$id = $this->convert_uid_to_id( $uid );
return $id;
}
}
foreach ( $saved_links as $link ) {
if ( $link->url === $url ) {
$id = $link->id;
$name = $link->linkTitle;
return $id;
}
}
}
}