forked from pixelgrade/pixtypes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-pixtypes.php
586 lines (507 loc) · 18 KB
/
class-pixtypes.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
<?php
/**
* PixTypes.
*
* @package PixTypes
* @author Pixelgrade <contact@pixelgrade.com>
* @license GPL-2.0+
* @link http://pixelgrade.com
* @copyright 2013 Pixelgrade
*/
/**
* Plugin class.
*
* @package PixTypes
* @author Pixelgrade <contact@pixelgrade.com>
*/
class PixTypesPlugin {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @const string
*/
protected $version = '1.3.2';
/**
* 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 = 'pixtypes';
/**
* 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;
/**
* 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();
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// add_action( 'admin_init', array( $this, 'wpgrade_init_plugin' ) );
// Add the options page and menu item only when is needed.
if ( isset($this->config['display_settings']) && $this->config['display_settings'] ) {
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__ ) . 'pixtypes.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' ) );
// add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'plugins_loaded', array( $this, 'register_metaboxes'), 14 );
add_action( 'init', array( $this, 'register_entities'), 99999);
add_action( 'init', array( $this, 'theme_version_check'));
/**
* Ajax Callbacks
*/
add_action('wp_ajax_unset_pixtypes', array(&$this, 'ajax_unset_pixtypes'));
add_action('wp_ajax_nopriv_unset_pixtypes', array(&$this, 'ajax_unset_pixtypes'));
}
/**
* 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';
}
public function wpgrade_init_plugin(){
// $this->plugin_textdomain();
// $this->add_wpgrade_shortcodes_button();
}
/**
* 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 ) {
$config = self::config();
/** get options defined by themes */
$theme_types = get_option('pixtypes_themes_settings');
$types_settings = get_option($config['settings-key']);
$current_theme = '_pixtypes_theme';
// init settings
if ( empty($theme_types) ) {
$theme_types = array();
}
if ( empty($types_settings) ) {
$types_settings = array('themes' => array());
}
/** A pixelgrade theme will always have this class so we know we can import new settings **/
if (class_exists('wpgrade') ) {
$current_theme = wpgrade::shortname() . $current_theme;
// also inform the plugin about theme version
$types_settings['wpgrade_theme_version'] = wpgrade::themeversion();
} else {
$theme_types = self::get_defaults( 'pixtypes' . $current_theme );
}
if ( !empty($theme_types) ) {
foreach ( $theme_types as $theme_key => $theme) {
$theme_name = str_replace('_pixtypes_theme', '', $theme_key);
/** Process each post type's arguments **/
if ( $theme_key == $current_theme ) {
/** POST TYPES slugs **/
if (!empty( $theme_types[$current_theme]['post_types']) ){
foreach ( $theme_types[$current_theme]['post_types'] as $key => $post_type ) {
$testable_slug = $is_slug_unique = '';
$testable_slug = str_replace ( $theme_name.'-', '', $post_type["rewrite"]["slug"]);
/** for our current theme we try to prioritize slugs */
if ( isset( $post_type["rewrite"] ) && self::is_custom_post_type_slug_unique($testable_slug) ) {
/** this slug is unique we can quit the theme suffix */
$theme_types[$current_theme]['post_types'][$key]["rewrite"]["slug"] = $testable_slug;
}
// process menu icon if it exists
if ( isset($post_type['menu_icon']) ) {
$theme_types[$current_theme]['post_types'][$key]['menu_icon'] = plugins_url( 'assets/' . $post_type['menu_icon'] , __FILE__ ) ;
}
}
}
/** TAXONOMIES slugs **/
if (!empty( $theme_types[$current_theme]['taxonomies'] ) ) {
foreach ( $theme_types[$current_theme]['taxonomies'] as $key => $tax ) {
$testable_slug = $is_slug_unique = '';
$testable_slug = str_replace ( $theme_name.'-', '', $tax["rewrite"]["slug"]);
if ( isset( $tax["rewrite"] ) && self::is_tax_slug_unique($testable_slug) ) {
/** this slug is unique we can quit the theme suffix */
$theme_types[$current_theme]['taxonomies'][$key]["rewrite"]["slug"] = $testable_slug;
}
}
}
}
$types_settings['themes'][$theme_name] = $theme_types[$theme_key];
}
}
update_option($config['settings-key'], $types_settings);
// flush_rewrite_rules();
// global $wp_rewrite;
// $wp_rewrite->generate_rewrite_rules();
// flush_rewrite_rules();
/**
* http://wordpress.stackexchange.com/questions/36152/flush-rewrite-rules-not-working-on-plugin-deactivation-invalid-urls-not-showing
* nothing from above works in plugin so ...
*/
delete_option('rewrite_rules');
}
/**
* 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;
}
$screen = get_current_screen();
if ( $screen->id == $this->plugin_screen_hook_suffix ) {
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();
if ( $screen->id == $this->plugin_screen_hook_suffix ) {
wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), $this->version );
wp_localize_script( $this->plugin_slug . '-admin-script', 'locals',
array(
'ajax_url' => admin_url( 'admin-ajax.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(), $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 );
}
/**
* 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(
__( 'PixTypes', $this->plugin_slug ),
__( 'PixTypes', $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=pixtypes' ) . '">' . __( 'Settings', $this->plugin_slug ) . '</a>' ), $links );
}
function register_entities(){
require_once( $this->plugin_basepath . 'features/custom-entities.php' );
}
function register_metaboxes(){
require_once( $this->plugin_basepath . 'features/metaboxes/metaboxes.php' );
}
/**
* Check if this post_type's slug is unique
* @param $slug string
* @return boolean
*/
static function is_custom_post_type_slug_unique( $slug ){
global $wp_post_types;
$is_unique = true; /** Suppose it's true */
foreach ( $wp_post_types as $key => $post_type){
$rewrite = $post_type->rewrite;
/** if this post_type has a rewrite rule check for it */
if ( !empty( $rewrite ) && isset($rewrite["slug"]) && $slug == $rewrite["slug"] ){
$is_unique = false;
} elseif ( $slug == $key ) { /** the post_type doesn't have a slug param, so the slug is the name itself */
$is_unique = false;
}
}
return $is_unique;
}
/**
* Check if this taxnonomie's slug is unique
* @param $slug string
* @return boolean
*/
static function is_tax_slug_unique( $slug ){
global $wp_taxonomies;
$is_unique = true; /** Suppose it's true */
foreach ( $wp_taxonomies as $key => $tax){
$rewrite = $tax->rewrite;
/** if this post_type has a rewrite rule check for it */
if ( !empty( $rewrite ) && isset($rewrite["slug"]) && $slug == $rewrite["slug"] ){
$is_unique = false;
} elseif ( $slug == $key ) { /** the post_type doesn't have a slug param, so the slug is the name itself */
$is_unique = false;
}
}
return $is_unique;
}
static function get_defaults( $theme_key ) {
$types_options = array();
$types_options[$theme_key] = array();
$types_options[$theme_key]['post_types'] = array(
'pix_portfolio' => array(
'labels' => array (
'name' => __('Project', 'pixtypes_txtd'),
'singular_name' => __('Project', 'pixtypes_txtd'),
// 'add_new' => __('Add New', 'pixtypes_txtd'),
'add_new_item' => __('Add New Project', 'pixtypes_txtd'),
'edit_item' => __('Edit Project', 'pixtypes_txtd'),
'new_item' => __('New Project', 'pixtypes_txtd'),
'all_items' => __('All Projects', 'pixtypes_txtd'),
'view_item' => __('View Project', 'pixtypes_txtd'),
'search_items' => __('Search Projects', 'pixtypes_txtd'),
'not_found' => __('No Project found', 'pixtypes_txtd'),
'not_found_in_trash' => __('No Project found in Trash', 'pixtypes_txtd'),
'menu_name' => __('Projects', 'pixtypes_txtd'),
),
'public' => true,
'rewrite' => array (
'slug' => 'portfolio',
'with_front' => false,
),
'has_archive' => 'portfolio-archive',
'menu_icon' => plugins_url( 'assets/report.png' , __FILE__ ),
'supports' => array ( 'title', 'editor', 'thumbnail', 'page-attributes', 'excerpt'),
'yarpp_support' => true,
),
'pix_gallery' => array(
'labels' => array (
'name' => __('Gallery', 'pixtypes_txtd'),
'singular_name' => __('Gallery', 'pixtypes_txtd'),
'add_new' => __('Add New', 'pixtypes_txtd'),
'add_new_item' => __('Add New Gallery', 'pixtypes_txtd'),
'edit_item' => __('Edit Gallery', 'pixtypes_txtd'),
'new_item' => __('New Gallery', 'pixtypes_txtd'),
'all_items' => __('All Galleries', 'pixtypes_txtd'),
'view_item' => __('View Gallery', 'pixtypes_txtd'),
'search_items' => __('Search Galleries', 'pixtypes_txtd'),
'not_found' => __('No Gallery found', 'pixtypes_txtd'),
'not_found_in_trash' => __('No Gallery found in Trash', 'pixtypes_txtd'),
'menu_name' => __('Galleries', 'pixtypes_txtd'),
),
'public' => true,
'rewrite' => array (
'slug' => 'galleries',
'with_front' => false,
),
'has_archive' => 'galleries-archive',
'menu_position' => NULL,
'supports' => array ( 'title', 'thumbnail', 'page-attributes', 'excerpt'),
'yarpp_support' => true,
),
);
/** TAXONOMIES **/
$types_options[$theme_key]['taxonomies'] = array(
'pix_portfolio_categories' => array(
'hierarchical' => true,
'labels' => array (
'name' => __('Portfolio Categories', 'pixtypes_txtd'),
'singular_name' => __('Portfolio Category', 'pixtypes_txtd'),
'search_items' => __('Search Portfolio Category', 'pixtypes_txtd'),
'all_items' => __('All Portfolio Categories', 'pixtypes_txtd'),
'parent_item' => __('Parent Portfolio Category', 'pixtypes_txtd'),
'parent_item_colon' => __('Parent Portfolio Category: ', 'pixtypes_txtd'),
'edit_item' => __('Edit Portfolio Category', 'pixtypes_txtd'),
'update_item' => __('Update Portfolio Category', 'pixtypes_txtd'),
'add_new_item' => __('Add New Portfolio Category', 'pixtypes_txtd'),
'new_item_name' => __('New Portfolio Category Name', 'pixtypes_txtd'),
'menu_name' => __('Portfolio Categories', 'pixtypes_txtd'),
),
'show_admin_column' => true,
'rewrite' => array ( 'slug' => 'portfolio-category', 'with_front' => false ),
'sort' => true,
'post_types' => array('pix_portfolio')
),
'pix_gallery_categories' => array(
'hierarchical' => true,
'labels' => array (
'name' => __('Gallery Categories', 'pixtypes_txtd'),
'singular_name' => __('Gallery Category', 'pixtypes_txtd'),
'search_items' => __('Search Gallery Category', 'pixtypes_txtd'),
'all_items' => __('All Gallery Categories', 'pixtypes_txtd'),
'parent_item' => __('Parent Gallery Category', 'pixtypes_txtd'),
'parent_item_colon' => __('Parent Gallery Category: ', 'pixtypes_txtd'),
'edit_item' => __('Edit Gallery Category', 'pixtypes_txtd'),
'update_item' => __('Update Gallery Category', 'pixtypes_txtd'),
'add_new_item' => __('Add New Gallery Category', 'pixtypes_txtd'),
'new_item_name' => __('New Gallery Category Name', 'pixtypes_txtd'),
'menu_name' => __('Gallery Categories', 'pixtypes_txtd'),
),
'show_admin_column' => true,
'rewrite' => array ( 'slug' => 'gallery-category', 'with_front' => false ),
'sort' => true,
'post_types' => array('pix_gallery')
),
);
/** METABOXES **/
$types_options[$theme_key]['metaboxes'] = array(
'pix_portfolio' => array(
'id' => 'portfolio_gallery',
'title' => __('Gallery', 'pixtypes_txtd'),
'pages' => array( 'pix_portfolio' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => __('Images', 'pixtypes_txtd'),
'id' => 'pix_portfolio_gallery',
'type' => 'gallery',
)
)
),
'pix_gallery' => array(
'id' => 'pix_gallery',
'title' => __('Gallery', 'pixtypes_txtd'),
'pages' => array( 'pix_gallery' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => __('Images', 'pixtypes_txtd'),
'id' => 'pix_main_gallery',
'type' => 'gallery',
)
)
)
);
return $types_options;
}
/**
* Ajax callback for unseting unneeded post types
*/
function ajax_unset_pixtypes(){
$result = array('success' => false, 'msg' => 'Incorect nonce');
if ( !wp_verify_nonce($_POST['_ajax_nonce'], 'unset_pixtype') ) {
echo json_encode($result);
die();
}
if ( isset($_POST['post_type']) ) {
$key = $_POST['post_type'];
$options = get_option('pixtypes_settings');
if ( isset( $options['themes'][$key] ) ) {
unset($options['themes'][$key]);
update_option('pixtypes_settings', $options);
$result['msg'] = 'Post type ' . $key . ' is gone.';
$result['success'] = true;
}
}
echo json_encode( $result );
exit;
}
/**
* On every wpgrade themes update we need to reconvert theme options into plugin options
*/
function theme_version_check(){
$options = get_option('pixtypes_settings');
if ( class_exists( 'wpgrade' ) && isset($options['wpgrade_theme_version']) ) {
if ( wpgrade::themeversion() != $options['wpgrade_theme_version'] ) {
// here the theme is updating it's options
$test = function_exists('wpgrade_callback_geting_active');
wpgrade_callback_geting_active();
// the plugin will copy these options into it's own field
self::activate(false);
// end finally merge user's settings with the theme ones
save_pixtypes_settings($options);
}
}
}
function get_plugin_version() {
return $this->version;
}
}