-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-pixfields.php
718 lines (568 loc) · 20.3 KB
/
class-pixfields.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
/**
* PixFields.
* @package PixFields
* @author Pixelgrade <[email protected]>
* @license GPL-2.0+
* @link http://pixelgrade.com
* @copyright 2014 Pixelgrade
*/
/**
* Plugin class.
* @package PixFields
* @author Pixelgrade <[email protected]>
*/
class PixFieldsPlugin {
/**
* Plugin version, used for cache-busting of style and script file references.
* @since 1.0.0
* @const string
*/
protected $version = '0.7.1';
/**
* Unique identifier for your plugin.
* Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
* match the Text Domain file header in the main plugin file.
* @since 1.0.0
* @var string
*/
protected $plugin_slug = 'pixfields';
/**
* Instance of this class.
* @since 1.0.0
* @var object
*/
protected static $instance = null;
/**
* Slug of the plugin screen.
* @since 1.0.0
* @var string
*/
protected $plugin_screen_hook_suffix = null;
/**
* Path to the plugin.
* @since 1.0.0
* @var string
*/
protected $plugin_basepath = null;
public $display_admin_menu = false;
protected $config;
protected static $number_of_images;
public $plugin_settings = null;
public static $fields_list = null;
/**
* Initialize the plugin by setting localization, filters, and administration functions.
* @since 1.0.0
*/
protected function __construct() {
$this->plugin_basepath = plugin_dir_path( __FILE__ );
$this->config = self::config();
$this->get_pixfields_list();
$this->get_plugin_settings();
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
// Add an action link pointing to the options page.
$plugin_basename = plugin_basename( plugin_dir_path( __FILE__ ) . 'pixfields.php' );
add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) );
// Load admin style sheet and JavaScript.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
// Load public-facing style sheet and JavaScript.
// add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ), 99999999999 );
// add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// add the metabox
// modal
add_action( 'add_meta_boxes', array( $this, 'pixfields_add_modal_meta_box' ) );
add_action( 'save_post', array( $this, 'pixfields_save_meta_data' ) );
// fields
add_action( 'add_meta_boxes', array( $this, 'add_pixfields_meta_box' ) );
// add_action( 'save_post', array( $this, 'pixfields_save_modal_data' ) );
//@deprecated
//add_filter( 'icl_wpml_config_array', array( $this, 'add_fields_to_the_wpml_options' ), 1 );
// a little hook into the_content
add_filter( 'the_content', array( $this, 'hook_into_the_content' ), 10, 1 );
/**
* Ajax Callbacks
*/
add_action( 'wp_ajax_save_pixfields', array( $this, 'ajax_save_pixfields' ) );
add_action( 'wp_ajax_pixfield_autocomplete', array( $this, 'ajax_pixfield_autocomplete' ) );
// only admins can access this
add_action( 'wp_ajax_nopriv_save_pixfields', array( $this, 'ajax_no_access' ) );
add_action( 'wp_ajax_nopriv_pixfield_autocomplete', array( $this, 'ajax_no_access' ) );
}
/**
* Return an instance of this class.
* @since 1.0.0
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
public static function config() {
// @TODO maybe check this
return include 'plugin-config.php';
}
/**
* Fired when the plugin is activated.
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
*/
public static function activate( $network_wide ) {
}
/**
* Fired when the plugin is deactivated.
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
*/
static function deactivate( $network_wide ) {
// TODO: Define deactivation functionality here
}
/**
* Load the plugin text domain for translation.
* @since 1.0.0
*/
function load_plugin_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/lang/' );
}
/**
* Register and enqueue admin-specific style sheet.
* @since 1.0.0
* @return null Return early if no settings page is registered.
*/
function enqueue_admin_styles() {
if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$current_post_type = get_post_type();
$is_post_page = false;
if ( $current_post_type && isset( $this->plugin_settings['display_on_post_types'] ) ) {
$is_post_page = array_key_exists( $current_post_type, $this->plugin_settings['display_on_post_types'] );
}
$screen = get_current_screen();
if ( $screen->id == $this->plugin_screen_hook_suffix || $is_post_page ) {
wp_enqueue_style( $this->plugin_slug . '-admin-styles', plugins_url( 'css/admin.css', __FILE__ ), array(), $this->version );
}
}
/**
* Register and enqueue admin-specific JavaScript.
* @since 1.0.0
* @return null Return early if no settings page is registered.
*/
function enqueue_admin_scripts() {
if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$screen = get_current_screen();
$current_post_type = get_post_type();
$is_post_page = false;
if ( $current_post_type && isset( $this->plugin_settings['display_on_post_types'] ) ) {
$is_post_page = array_key_exists( $current_post_type, $this->plugin_settings['display_on_post_types'] );
}
if ( $screen->id == $this->plugin_screen_hook_suffix || ( $is_post_page && $this->is_edit_page() ) ) {
wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery', 'jquery-ui-autocomplete', 'jquery-ui-sortable' ), $this->version );
$localized_array = array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
);
if ( isset( $this->plugin_settings['display_on_post_types'] ) || ! empty( $this->plugin_settings['display_on_post_types'] ) ) {
$localized_array['pixfields'] = $this->get_pixfields_list();
}
wp_localize_script( $this->plugin_slug . '-admin-script', 'pixfields_l10n',$localized_array);
}
}
function is_edit_page($new_edit = null){
global $pagenow;
//make sure we are on the backend
if (!is_admin()) return false;
if($new_edit == "edit")
return in_array( $pagenow, array( 'post.php', ) );
elseif($new_edit == "new") //check for new post page
return in_array( $pagenow, array( 'post-new.php' ) );
else //check for either new or edit
return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
}
/**
* Register and enqueue public-facing style sheet.
* @since 1.0.0
*/
function enqueue_styles() {
// wp_enqueue_style( $this->plugin_slug . '-plugin-styles', plugins_url( 'css/public.css', __FILE__ ), array('wpgrade-main-style'), $this->version );
}
/**
* Register and enqueues public-facing JavaScript files.
* @since 1.0.0
*/
public function enqueue_scripts() {
//wp_enqueue_script( $this->plugin_slug . '-plugin-script', plugins_url( 'js/public.js', __FILE__ ), array( 'jquery' ), $this->version, true );
}
/**
* Register the administration menu for this plugin into the WordPress Dashboard menu.
*/
function add_plugin_admin_menu() {
$this->plugin_screen_hook_suffix = add_options_page( __( 'PixFields', $this->plugin_slug ), __( 'PixFields', $this->plugin_slug ), 'manage_options', $this->plugin_slug, array(
$this,
'display_plugin_admin_page'
) );
}
/**
* Render the settings page for this plugin.
*/
function display_plugin_admin_page() {
include_once( 'views/admin.php' );
}
/**
* Add settings action link to the plugins page.
*/
function add_action_links( $links ) {
return array_merge( array( 'settings' => '<a href="' . admin_url( 'options-general.php?page=pixfields' ) . '">' . __( 'Settings', $this->plugin_slug ) . '</a>' ), $links );
}
/**
* Adds a box to the main column on any post type checked in settings
*/
function add_pixfields_meta_box() {
if ( ! isset( $this->plugin_settings['display_on_post_types'] ) || empty( $this->plugin_settings['display_on_post_types'] ) ) {
return;
}
foreach ( $this->plugin_settings['display_on_post_types'] as $post_type => $val ) {
// Make a nice metabox title
$post_type_obj = get_post_type_object($post_type);
$post_type_name = $post_type;
if ( $post_type_obj !== null ) {
$post_type_name = $post_type_obj->labels->singular_name;
}
add_meta_box(
'pixfields',
$post_type_name . __( ' fields', 'pixfields_txtd' ),
array( $this, 'pixfields_meta_box_callback' ),
$post_type,
'side'
);
}
}
function pixfields_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'pixfields_meta_box', 'pixfields_meta_box_nonce' );
// these settings depend on post type
$post_type = $post->post_type;
if ( empty( $post_type ) ) {
return false;
} ?>
<ul class="pixfields" data-post_type="<?php echo $post_type; ?>">
<?php // check if we have fields for this post type
$fields_list = $this->get_pixfields_list();
if ( isset( $fields_list[$post->post_type] ) && ! empty( $fields_list[$post->post_type] ) ) {
foreach ( $fields_list[$post->post_type] as $key => $field ) {
$meta_key = 'pixfield_' . $field['meta_key'];
$value = get_post_meta( $this->get_the_post_id( $post->ID, $post->post_type ), $meta_key, true); ?>
<li class="pixfield" data-pixfield="<?php echo $meta_key ?>">
<label for="<?php echo $meta_key; ?>"><?php echo $field['label'];?></label>
<br/>
<input type="text" class="pixfield_value" name="<?php echo $meta_key; ?>" <?php echo ( !empty( $value ) ) ? 'value="'.$value . '"' :''; ?>/>
</li>
<?php } ?>
<?php
} ?>
</ul>
<?php
if ( isset( $this->plugin_settings['allow_edit_on_post_page'] ) && $this->plugin_settings['allow_edit_on_post_page'] ) { ?>
<span class="manage_button_wrapper">
<a href="#" class="open_pixfields_modal"><?php _e( 'Manage fields', 'pixfields_txtd' ); ?></a>
</span>
<?php }
}
function get_the_post_id( $id, $post_type = 'post' ) {
if(function_exists('icl_object_id')) {
return icl_object_id($id, $post_type, true);
} else {
return $id;
}
}
/**
* When the post is saved, saves our custom data.
* @param int $post_id The ID of the post being saved.
*/
function pixfields_save_meta_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set and if it's valid.
if ( ! isset( $_POST['pixfields_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['pixfields_meta_box_nonce'], 'pixfields_meta_box' ) ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['pixfields_list'] ) || ! is_array( $_POST['pixfields_list'] ) ) {
return;
}
global $post;
// get only our pixfields keys and values #regex #hack #danger
$meta_keys = self::get_pixfields_keys( $post->post_type );
$pixfield_keys = array_intersect_key( $_POST, $meta_keys );
foreach ( $pixfield_keys as $key => $value ) {
update_post_meta( $post_id, $key, $value );
}
}
function pixfields_add_modal_meta_box() {
if ( ! isset( $this->plugin_settings['display_on_post_types'] ) || empty( $this->plugin_settings['display_on_post_types'] ) ) {
return;
}
foreach ( $this->plugin_settings['display_on_post_types'] as $post_type => $val ) {
add_meta_box(
'pixfields_manager',
__( 'PixFields', 'pixfields_txtd' ),
array( $this, 'modal_meta_box_callback' ),
$post_type
);
}
}
function modal_meta_box_callback ( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'pixfields_modal_meta_box', 'pixfields_modal_meta_box_nonce' ); ?>
<div class="pixfields_manager_modal">
<?php
$config = include pixfields::pluginpath() . 'plugin-config' . EXT;
$processor = pixfields::processor( $config );
$f = pixfields::form( $config, $processor ); ?>
<div class="pixfields_form">
<?php echo $f->field( 'fields_manager' )->render(); ?>
</div>
</div>
<?php }
function ajax_no_access() {
echo 'you have no access here';
die();
}
function ajax_save_pixfields(){
ob_start();
if ( isset( $_REQUEST['fields'] ) ) {
$fields_string = $_REQUEST['fields'];
} else {
wp_send_json_error( 'No fields sent' );
exit;
}
$post = get_post( $_REQUEST['post_id'] );
parse_str($fields_string, $fields);
if ( ! isset ( $fields['pixfields_list'] ) ) {
$fields['pixfields_list'] = array( $post->post_type => array() );
}
$this->make_fields( $post->post_type, $fields['pixfields_list'] );
echo $this->pixfields_meta_box_callback( $post );
$out = ob_get_clean();
wp_send_json_success($out);
exit;
}
function ajax_pixfield_autocomplete() {
ob_start();
if ( ! isset( $_REQUEST['post_type'] ) && ! isset( $_REQUEST['pixfield'] ) && ! isset( $_REQUEST['value'] ) ) {
wp_send_json_error( 'No data recived' );
exit;
}
$values = $this->get_meta_values( $_REQUEST['pixfield'], $_REQUEST['post_type'] );
echo json_encode($values);
exit;
}
function make_fields( $post_type, $pixfields_list ) {
if ( ! empty ( $pixfields_list ) ) {
foreach ( $pixfields_list as $post_type => $fields ){
// check if this post type has fields
if ( empty( $fields ) ) {
self::$fields_list[$post_type] = array();
continue;
}
// make a list with all meta_keys already existent ( they should already be unique )
$unique_meta_keys = array();
foreach ( $fields as $key => $field ) {
if ( isset( $field['meta_key'] ) ) {
$unique_meta_keys[ $field['meta_key'] ] = $field['meta_key'];
}
}
$fields = array_values( $fields );
foreach ( $fields as $key => $field ) {
$fields[$key] = array_map('sanitize_text_field', $field );
// if we don't have a meta key this means this is a new field
// so we ensure there isn't already a meta_key with the same value
if ( ! isset( $field['meta_key'] ) ) {
$meta_key = $field['label']; // old way sanitize_title_with_dashes( $field['label'] );
// try to sanitize a little
$meta_key = strip_tags($meta_key);
$meta_key = strtolower($meta_key);
$meta_key = preg_replace('/&.+?;/', '', $meta_key); // kill entities
$meta_key = str_replace(array(' ', '.', '!', '?', ',', ':' ), '-', $meta_key);
// but if it is we make it unique with a timestamp suffix
if ( in_array($meta_key, $unique_meta_keys) ) {
$meta_key = $meta_key . '-'. time();
}
array_push($unique_meta_keys, $meta_key);
$fields[$key]['meta_key'] = $meta_key;
}
}
self::$fields_list[$post_type] = $fields;
}
// Update the meta field in the database.
update_option('pixfields_list', self::$fields_list);
} else {
self::$fields_list[ $post_type ] = array();
update_option('pixfields_list', self::$fields_list[ $post_type ] );
}
}
function hook_into_the_content( $content ) {
if ( ! isset( $this->plugin_settings['display_place'] ) ) {
return $content;
}
// when we have a shotcode this is not needed anymore
if ( has_shortcode( $content, 'pixfields' ) ) {
return $content;
}
global $post;
$metadata = $this->get_template( $post->ID );
if ( $this->plugin_settings['display_place'] == 'after_content' ) {
return $content . $metadata;
} elseif ( $this->plugin_settings['display_place'] == 'before_content') {
return $metadata . $content;
}
return $content;
}
public function get_template( $post_id = null ) {
if ( $post_id == null ) {
$post = get_post( $post_id );
} else {
global $post;
}
$template_name = 'pixfields_box' . EXT;
$_located = locate_template( "templates/" . $template_name, false, false );
// use the default one if the (child) theme doesn't have it
if ( ! $_located ) {
$_located = dirname( __FILE__ ) . '/views/' . $template_name;
}
$pixfields = $this->get_post_pixfields( $post->post_type, $post->ID );
ob_start();
require $_located;
return ob_get_clean();
}
public function get_post_pixfields( $post_type, $post_id ){
$fields_list = $this->get_pixfields_list();
$keys = array();
if ( isset($fields_list[$post_type] ) ) {
foreach ($fields_list[$post_type] as $field ) {
$keys[ $field['meta_key'] ] = array(
'label' => $field['label'],
'value' => get_post_meta( $post_id, 'pixfield_' . $field['meta_key'], true),
);
if ( isset( $field['filter'] ) ) {
$keys[ $field['meta_key'] ]['filter'] = $field['filter'];
}
}
}
return $keys;
}
static function get_pixfields_keys( $post_type ) {
if ( isset( self::$fields_list[$post_type] ) ) {
$keys = array();
foreach ( self::$fields_list[$post_type] as $field ) {
$keys[ 'pixfield_' . $field['meta_key'] ] = '';
}
return $keys;
}
return false;
}
function update_plugin_setting( $name, $value ) {
// it doesn't matter if the settings doesn't exist we create one then
$this->plugin_settings[ $name ] = $value;
update_option( 'pixfields_settings', $this->plugin_settings );
}
public function get_plugin_settings() {
if ( $this->plugin_settings === null ) {
$this->plugin_settings = get_option( 'pixfields_settings' );
}
return $this->plugin_settings;
}
public function get_pixfields_list(){
if ( self::$fields_list === null ) {
self::$fields_list = get_option( 'pixfields_list' );
}
return self::$fields_list;
}
/**
* @param $options
*
* @return mixed
* @deprecated
*/
// inform WPML about PixField's labels and pass them as admin-texts
function add_fields_to_the_wpml_options( $options ) {
if ( isset( $options['wpml-config']['admin-texts'] ) ) {
$pixfields_list = get_option('pixfields_list');
// add pixfields labels to the wpml config list
$pixfields_wpml_config = array(
'attr' => array( 'name' => 'pixfields_list' )
);
if ( ! empty( $pixfields_list ) ) {
foreach ( $pixfields_list as $post_type => $pixfields ) {
$these_args = array(
'attr' => array( 'name' => $post_type )
);
foreach ( $pixfields as $pkey => $pixfield ) {
$these_args['key'][] = array(
'attr' => array( 'name' => $pkey ),
'key' => array( 'attr' => array( 'name' => 'label' ) )
);
}
$pixfields_wpml_config['key'][] = $these_args;
}
}
$pixfields_wpml_config['type'] = 'plugin';
$pixfields_wpml_config['context'] = 'pixfields';
$options['wpml-config']['admin-texts']['key'][] = $pixfields_wpml_config;
}
return $options;
}
function get_meta_values( $key = '', $type = 'post', $status = 'publish' ) {
if( empty( $key ) )
return;
//first get all posts of a certain type
$args = array(
'numberposts' => -1,
'post_type' => $type,
'post_status' => $status,
'suppress_filters' => false, //allow filters - like WPML
);
$posts = get_posts($args);
//now go through each and get the meta value for the given meta key
if ( ! empty( $posts ) ) {
foreach ( $posts as $post) {
$meta_value = get_post_meta($post->ID, $key, true);
if ( ! empty($meta_value) && strlen($meta_value) < 26 ) { //only 25 characters max
$r[] = $meta_value;
}
}
}
if ( !empty($r) ) {
$r = array_filter( $r );
return array_combine($r, $r);
}
return false;
}
static function get_base_path() {
return plugin_dir_path( __FILE__ );
}
}