| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
/** |
| 4 |
* PixTypes. |
| 5 |
* |
| 6 |
* @package PixTypes |
| 7 |
* @author Pixelgrade <contact@pixelgrade.com> |
| 8 |
* @license GPL-2.0+ |
| 9 |
* @link https://pixelgrade.com |
| 10 |
* @copyright 2013-2017 Pixelgrade |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Plugin class. |
| 15 |
* |
| 16 |
* @package PixTypes |
| 17 |
* @author Pixelgrade <contact@pixelgrade.com> |
| 18 |
*/ |
| 19 |
class PixTypesPlugin { |
| 20 |
|
| 21 |
/** |
| 22 |
* Plugin version, used for cache-busting of style and script file references. |
| 23 |
* |
| 24 |
* @since 1.0.0 |
| 25 |
* |
| 26 |
* @const string |
| 27 |
*/ |
| 28 |
protected $version; |
| 29 |
/** |
| 30 |
* Unique identifier for your plugin. |
| 31 |
* |
| 32 |
* Use this value (not the variable name) as the text domain when internationalizing strings of text. It should |
| 33 |
* match the Text Domain file header in the main plugin file. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $plugin_slug = 'pixtypes'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Instance of this class. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* |
| 46 |
* @var object |
| 47 |
*/ |
| 48 |
protected static $instance = null; |
| 49 |
|
| 50 |
/** |
| 51 |
* Slug of the plugin screen. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
protected $plugin_screen_hook_suffix = null; |
| 58 |
|
| 59 |
/** |
| 60 |
* Path to the plugin. |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* @var string |
| 64 |
*/ |
| 65 |
protected $plugin_basepath = null; |
| 66 |
|
| 67 |
public $display_admin_menu = false; |
| 68 |
|
| 69 |
protected $config; |
| 70 |
|
| 71 |
/** |
| 72 |
* Initialize the plugin by setting localization, filters, and administration functions. |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
* |
| 76 |
* @param string $version |
| 77 |
*/ |
| 78 |
protected function __construct( $version = '1.0.0' ) { |
| 79 |
$this->version = $version; |
| 80 |
$this->plugin_basepath = plugin_dir_path( __FILE__ ); |
| 81 |
$this->config = self::config(); |
| 82 |
|
| 83 |
// Load plugin text domain |
| 84 |
add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); |
| 85 |
|
| 86 |
// Add the options page and menu item only when is needed. |
| 87 |
if ( isset( $this->config['display_settings'] ) && $this->config['display_settings'] ) { |
| 88 |
add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) ); |
| 89 |
|
| 90 |
// Add an action link pointing to the options page. |
| 91 |
$plugin_basename = plugin_basename( plugin_dir_path( __FILE__ ) . 'pixtypes.php' ); |
| 92 |
add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
// Load admin stylesheet and JavaScript files. |
| 97 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 98 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
| 99 |
|
| 100 |
add_action( 'plugins_loaded', array( $this, 'register_metaboxes' ), 14 ); |
| 101 |
add_action( 'init', array( $this, 'register_entities' ), 99999 ); |
| 102 |
|
| 103 |
// We need this later then the default 10 priority so we can have things happening between the init-10 and the PixTypes config |
| 104 |
add_action( 'init', array( $this, 'theme_version_check' ), 15 ); |
| 105 |
|
| 106 |
/** |
| 107 |
* Ajax Callbacks - only for logged in users |
| 108 |
*/ |
| 109 |
add_action( 'wp_ajax_unset_pixtypes', array( $this, 'ajax_unset_pixtypes' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Return an instance of this class. |
| 114 |
* |
| 115 |
* @since 1.0.0 |
| 116 |
* |
| 117 |
* @param string $version The current plugin version. |
| 118 |
* @return object A single instance of this class. |
| 119 |
*/ |
| 120 |
public static function get_instance( $version = '1.0.0' ) { |
| 121 |
|
| 122 |
// If the single instance hasn't been set, set it now. |
| 123 |
if ( null == self::$instance ) { |
| 124 |
self::$instance = new self( $version ); |
| 125 |
} |
| 126 |
|
| 127 |
return self::$instance; |
| 128 |
} |
| 129 |
|
| 130 |
public static function config() { |
| 131 |
// @TODO maybe check this |
| 132 |
return include 'plugin-config.php'; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Fired when the plugin is activated. |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* |
| 140 |
* @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. |
| 141 |
*/ |
| 142 |
public static function activate( $network_wide ) { |
| 143 |
$config = self::config(); |
| 144 |
|
| 145 |
/** get options defined by themes */ |
| 146 |
$theme_types = get_option( 'pixtypes_themes_settings' ); |
| 147 |
$types_settings = get_option( $config['settings-key'] ); |
| 148 |
$current_theme = '_pixtypes_theme'; |
| 149 |
|
| 150 |
// init settings |
| 151 |
if ( empty( $theme_types ) ) { |
| 152 |
$theme_types = array(); |
| 153 |
} |
| 154 |
|
| 155 |
if ( empty( $types_settings ) ) { |
| 156 |
$types_settings = array( 'themes' => array() ); |
| 157 |
} |
| 158 |
|
| 159 |
/** A pixelgrade theme will always have this class so we know we can import new settings **/ |
| 160 |
if ( class_exists( 'wpgrade' ) ) { |
| 161 |
|
| 162 |
$current_theme = wpgrade::shortname() . $current_theme; |
| 163 |
// also inform the plugin about theme version |
| 164 |
$types_settings['wpgrade_theme_version'] = wpgrade::themeversion(); |
| 165 |
|
| 166 |
} else { |
| 167 |
$theme_types = self::get_defaults( 'pixtypes' . $current_theme ); |
| 168 |
} |
| 169 |
|
| 170 |
if ( ! empty( $theme_types ) ) { |
| 171 |
foreach ( $theme_types as $theme_key => $theme ) { |
| 172 |
$theme_name = str_replace( '_pixtypes_theme', '', $theme_key ); |
| 173 |
/** Process each post type's arguments **/ |
| 174 |
if ( $theme_key == $current_theme ) { |
| 175 |
|
| 176 |
/** POST TYPES slugs **/ |
| 177 |
if ( ! empty( $theme_types[ $current_theme ]['post_types'] ) ) { |
| 178 |
foreach ( $theme_types[ $current_theme ]['post_types'] as $key => $post_type ) { |
| 179 |
$testable_slug = str_replace( $theme_name . '-', '', $post_type['rewrite']['slug'] ); |
| 180 |
|
| 181 |
/** for our current theme we try to prioritize slugs */ |
| 182 |
if ( isset( $post_type['rewrite'] ) && self::is_custom_post_type_slug_unique( $testable_slug ) ) { |
| 183 |
/** this slug is unique we can quit the theme suffix */ |
| 184 |
$theme_types[ $current_theme ]['post_types'][ $key ]['rewrite']['slug'] = $testable_slug; |
| 185 |
} |
| 186 |
|
| 187 |
// process menu icon if it exists |
| 188 |
if ( isset( $post_type['menu_icon'] ) ) { |
| 189 |
// If we have been given a dashicon, use it without processing |
| 190 |
if ( false !== strpos( $post_type['menu_icon'], 'dashicon' ) ) { |
| 191 |
$theme_types[ $current_theme ]['post_types'][ $key ]['menu_icon'] = $post_type['menu_icon']; |
| 192 |
} else { |
| 193 |
$theme_types[ $current_theme ]['post_types'][ $key ]['menu_icon'] = plugins_url( 'assets/' . $post_type['menu_icon'], __FILE__ ); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** TAXONOMIES slugs **/ |
| 200 |
if ( ! empty( $theme_types[ $current_theme ]['taxonomies'] ) ) { |
| 201 |
foreach ( $theme_types[ $current_theme ]['taxonomies'] as $key => $tax ) { |
| 202 |
$testable_slug = str_replace( $theme_name . '-', '', $tax['rewrite']['slug'] ); |
| 203 |
if ( isset( $tax['rewrite'] ) && self::is_tax_slug_unique( $testable_slug ) ) { |
| 204 |
/** this slug is unique we can quit the theme suffix */ |
| 205 |
$theme_types[ $current_theme ]['taxonomies'][ $key ]['rewrite']['slug'] = $testable_slug; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
$types_settings['themes'][ $theme_name ] = $theme_types[ $theme_key ]; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
update_option( $config['settings-key'], $types_settings ); |
| 215 |
|
| 216 |
/** |
| 217 |
* http://wordpress.stackexchange.com/questions/36152/flush-rewrite-rules-not-working-on-plugin-deactivation-invalid-urls-not-showing |
| 218 |
* nothing from above works in plugins so ... |
| 219 |
*/ |
| 220 |
delete_option( 'rewrite_rules' ); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Fired when the plugin is deactivated. |
| 225 |
* @since 1.0.0 |
| 226 |
* |
| 227 |
* @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. |
| 228 |
*/ |
| 229 |
static function deactivate( $network_wide ) { |
| 230 |
// TODO: Define deactivation functionality here |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Load the plugin text domain for translation. |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
*/ |
| 238 |
function load_plugin_textdomain() { |
| 239 |
|
| 240 |
$domain = $this->plugin_slug; |
| 241 |
$locale = apply_filters( 'plugin_locale', get_locale(), $domain ); |
| 242 |
|
| 243 |
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' ); |
| 244 |
load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/lang/' ); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Register and enqueue admin-specific style sheet. |
| 249 |
* |
| 250 |
* @since 1.0.0 |
| 251 |
* |
| 252 |
* @return null Return early if no settings page is registered. |
| 253 |
*/ |
| 254 |
function enqueue_admin_styles() { |
| 255 |
|
| 256 |
if ( ! isset( $this->plugin_screen_hook_suffix ) ) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
$screen = get_current_screen(); |
| 261 |
if ( $screen->id == $this->plugin_screen_hook_suffix ) { |
| 262 |
wp_enqueue_style( $this->plugin_slug . '-admin-styles', plugins_url( 'css/admin.css', __FILE__ ), array(), $this->version ); |
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Register and enqueue admin-specific JavaScript. |
| 269 |
* |
| 270 |
* @since 1.0.0 |
| 271 |
* |
| 272 |
* @return null Return early if no settings page is registered. |
| 273 |
*/ |
| 274 |
function enqueue_admin_scripts() { |
| 275 |
|
| 276 |
if ( ! isset( $this->plugin_screen_hook_suffix ) ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
$screen = get_current_screen(); |
| 281 |
if ( $screen->id == $this->plugin_screen_hook_suffix ) { |
| 282 |
wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), $this->version, true ); |
| 283 |
wp_localize_script( $this->plugin_slug . '-admin-script', 'locals', |
| 284 |
array( |
| 285 |
'ajax_url' => admin_url( 'admin-ajax.php' ) |
| 286 |
) |
| 287 |
); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Register the administration menu for this plugin into the WordPress Dashboard menu. |
| 293 |
*/ |
| 294 |
function add_plugin_admin_menu() { |
| 295 |
|
| 296 |
$this->plugin_screen_hook_suffix = add_options_page( |
| 297 |
esc_html__( 'PixTypes', 'pixtypes' ), |
| 298 |
esc_html__( 'PixTypes', 'pixtypes' ), |
| 299 |
'manage_options', |
| 300 |
$this->plugin_slug, |
| 301 |
array( $this, 'display_plugin_admin_page' ) |
| 302 |
); |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Render the settings page for this plugin. |
| 308 |
*/ |
| 309 |
function display_plugin_admin_page() { |
| 310 |
include_once( 'views/admin.php' ); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Add settings action link to the plugins page. |
| 315 |
*/ |
| 316 |
function add_action_links( $links ) { |
| 317 |
return array_merge( array( 'settings' => '<a href="' . admin_url( 'options-general.php?page=pixtypes' ) . '">' . esc_html__( 'Settings', 'pixtypes' ) . '</a>' ), $links ); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Extract the user-facing settings key from a registered entity slug. |
| 322 |
* |
| 323 |
* @param string $entity The post type or taxonomy slug. |
| 324 |
* @param string $delimiter The delimiter used to strip a theme/vendor prefix. |
| 325 |
* |
| 326 |
* @return string |
| 327 |
*/ |
| 328 |
protected function get_entity_settings_key( $entity, $delimiter ) { |
| 329 |
$entity = (string) $entity; |
| 330 |
$key = strstr( $entity, $delimiter ); |
| 331 |
|
| 332 |
if ( false === $key ) { |
| 333 |
return $entity; |
| 334 |
} |
| 335 |
|
| 336 |
$key = substr( $key, 1 ); |
| 337 |
|
| 338 |
if ( '' === $key ) { |
| 339 |
return $entity; |
| 340 |
} |
| 341 |
|
| 342 |
return $key; |
| 343 |
} |
| 344 |
|
| 345 |
function register_entities() { |
| 346 |
// register post types |
| 347 |
$options = $updated_options = apply_filters( 'pixtypes_settings_pre_register_entitites', get_option( 'pixtypes_settings' ) ); |
| 348 |
$updated_options['display_settings'] = false; |
| 349 |
|
| 350 |
// go through each theme and activate portfolio post types |
| 351 |
if ( empty( $options['themes'] ) || ! is_array( $options['themes'] ) ) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
/** A pixelgrade theme will always have this class so we know we can import new settings **/ |
| 356 |
$current_theme = false; |
| 357 |
if ( class_exists( 'wpgrade' ) ) { |
| 358 |
$current_theme = wpgrade::shortname(); |
| 359 |
} |
| 360 |
|
| 361 |
$theme_types = $options['themes']; |
| 362 |
foreach ( $theme_types as $key => $theme ) { |
| 363 |
// post types |
| 364 |
if ( isset( $theme['post_types'] ) && is_array( $theme['post_types'] ) ) { |
| 365 |
// Remember what post types we have removed from settings so we can skip registering their taxonomies |
| 366 |
$deleted_post_types = array(); |
| 367 |
foreach ( $theme['post_types'] as $post_type => $post_type_args ) { |
| 368 |
// First check if the post type is already registered - we bail if that is the case |
| 369 |
if ( post_type_exists( $post_type ) ) { |
| 370 |
continue; |
| 371 |
} |
| 372 |
|
| 373 |
// Second, for post types not belonging to the current theme, we only register them if there are posts |
| 374 |
if ( false !== $current_theme && $key != $current_theme ) { |
| 375 |
// We get the posts to see if there are any - we include all post statuses (trash and auto-draft need to be specified directly) |
| 376 |
$posts = get_posts( array( 'post_type' => $post_type, 'post_status' => array( 'any', 'trash', 'auto-draft' ), 'posts_per_page' => '1' ) ); |
| 377 |
if ( empty( $posts ) ) { |
| 378 |
// We should also delete the post type from the settings - no worries; if the theme gets activated again, all this will come back |
| 379 |
// This is just for auto-cleanup |
| 380 |
unset( $updated_options['themes'][ $key ]['post_types'][ $post_type ] ); |
| 381 |
$deleted_post_types[] = $post_type; |
| 382 |
continue; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
$is_jetpack_compatible = false; |
| 387 |
if ( strpos( $post_type, 'jetpack' ) !== false ) { |
| 388 |
///$xxxx = str_replace( 'jetpack-', '', $post_type); |
| 389 |
$is_jetpack_compatible = true; |
| 390 |
} |
| 391 |
|
| 392 |
if ( $is_jetpack_compatible ) { |
| 393 |
$post_type_key = $this->get_entity_settings_key( $post_type, '-' ); |
| 394 |
} else { |
| 395 |
// eliminate the theme prefix |
| 396 |
$post_type_key = $this->get_entity_settings_key( $post_type, '_' ); |
| 397 |
} |
| 398 |
|
| 399 |
// process menu icon if it exists |
| 400 |
if ( isset( $post_type_args['menu_icon'] ) ) { |
| 401 |
// If we have been given a dashicon or full URL, use it without processing |
| 402 |
if ( false === strpos( $post_type_args['menu_icon'], 'dashicon' ) && false === filter_var( $post_type_args['menu_icon'], FILTER_VALIDATE_URL ) ) { |
| 403 |
$post_type_args['menu_icon'] = plugins_url( 'assets/' . $post_type_args['menu_icon'], __FILE__ ); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if ( isset( $options[ 'enable_' . $post_type_key ] ) ) { |
| 408 |
$updated_options['display_settings'] = true; |
| 409 |
if ( $options[ 'enable_' . $post_type_key ] ) { |
| 410 |
register_post_type( $post_type, $post_type_args ); |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
// taxonomies |
| 417 |
if ( isset( $theme['taxonomies'] ) && is_array( $theme['taxonomies'] ) ) { |
| 418 |
foreach ( $theme['taxonomies'] as $tax => $tax_args ) { |
| 419 |
// First check if the taxonomy is already registered - we bail if that is the case |
| 420 |
if ( taxonomy_exists( $tax ) ) { |
| 421 |
continue; |
| 422 |
} |
| 423 |
|
| 424 |
// If we have deleted post types settings we need to skip registering their taxonomies |
| 425 |
if ( ! empty( $tax_args['post_types'] ) && ! empty( $deleted_post_types ) ) { |
| 426 |
if ( ! is_array( $tax_args['post_types'] ) ) { |
| 427 |
if ( in_array( $tax_args['post_types'], $deleted_post_types ) ) { |
| 428 |
continue; |
| 429 |
} |
| 430 |
} else { |
| 431 |
$temp = array_diff( $tax_args['post_types'], $deleted_post_types ); |
| 432 |
if ( empty( $temp ) ) { |
| 433 |
continue; |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
$tax_post_types = $tax_args['post_types']; |
| 439 |
// remove "post_types", isn't a register_taxonomy argument we are just using it for post type linking |
| 440 |
unset( $tax_args['post_types'] ); |
| 441 |
|
| 442 |
$is_jetpack_compatible = false; |
| 443 |
if ( strpos( $tax, 'jetpack' ) !== false ) { |
| 444 |
///$xxxx = str_replace( 'jetpack-', '', $tax); |
| 445 |
$is_jetpack_compatible = true; |
| 446 |
} |
| 447 |
|
| 448 |
if ( $is_jetpack_compatible ) { |
| 449 |
$tax_key = $this->get_entity_settings_key( $tax, '-' ); |
| 450 |
} else { |
| 451 |
// eliminate the theme prefix |
| 452 |
$tax_key = $this->get_entity_settings_key( $tax, '_' ); |
| 453 |
} |
| 454 |
|
| 455 |
if ( isset( $options[ 'enable_' . $tax_key ] ) ) { |
| 456 |
$updated_options['display_settings'] = true; |
| 457 |
if ( $options[ 'enable_' . $tax_key ] ) { |
| 458 |
register_taxonomy( $tax, $tax_post_types, $tax_args ); |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
// Only update if we have actually changed something |
| 466 |
if ( $options != $updated_options ) { |
| 467 |
update_option( 'pixtypes_settings', $updated_options ); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
function register_metaboxes() { |
| 472 |
require_once( $this->plugin_basepath . 'features/metaboxes/metaboxes.php' ); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Check if this post_type's slug is unique |
| 477 |
* |
| 478 |
* @param $slug string |
| 479 |
* |
| 480 |
* @return boolean |
| 481 |
*/ |
| 482 |
static function is_custom_post_type_slug_unique( $slug ) { |
| 483 |
|
| 484 |
global $wp_post_types; |
| 485 |
$is_unique = true; |
| 486 |
/** Suppose it's true */ |
| 487 |
|
| 488 |
foreach ( $wp_post_types as $key => $post_type ) { |
| 489 |
$rewrite = $post_type->rewrite; |
| 490 |
/** if this post_type has a rewrite rule check for it */ |
| 491 |
if ( ! empty( $rewrite ) && isset( $rewrite['slug'] ) && $slug == $rewrite['slug'] ) { |
| 492 |
$is_unique = false; |
| 493 |
} elseif ( $slug == $key ) { |
| 494 |
/** the post_type doesn't have a slug param, so the slug is the name itself */ |
| 495 |
$is_unique = false; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
return $is_unique; |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Check if this taxnonomie's slug is unique |
| 504 |
* |
| 505 |
* @param $slug string |
| 506 |
* |
| 507 |
* @return boolean |
| 508 |
*/ |
| 509 |
static function is_tax_slug_unique( $slug ) { |
| 510 |
global $wp_taxonomies; |
| 511 |
|
| 512 |
/** Suppose it's true */ |
| 513 |
$is_unique = true; |
| 514 |
|
| 515 |
foreach ( $wp_taxonomies as $key => $tax ) { |
| 516 |
$rewrite = $tax->rewrite; |
| 517 |
/** if this post_type has a rewrite rule check for it */ |
| 518 |
if ( ! empty( $rewrite ) && isset( $rewrite['slug'] ) && $slug == $rewrite['slug'] ) { |
| 519 |
$is_unique = false; |
| 520 |
} elseif ( $slug == $key ) { |
| 521 |
/** the post_type doesn't have a slug param, so the slug is the name itself */ |
| 522 |
$is_unique = false; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
return $is_unique; |
| 527 |
} |
| 528 |
|
| 529 |
static function get_defaults( $theme_key ) { |
| 530 |
|
| 531 |
$types_options = array(); |
| 532 |
$types_options[ $theme_key ] = array(); |
| 533 |
$types_options[ $theme_key ]['post_types'] = array( |
| 534 |
'pix_portfolio' => array( |
| 535 |
'labels' => array( |
| 536 |
'name' => esc_html__( 'Project', 'pixtypes' ), |
| 537 |
'singular_name' => esc_html__( 'Project', 'pixtypes' ), |
| 538 |
'add_new' => esc_html__( 'Add New', 'pixtypes' ), |
| 539 |
'add_new_item' => esc_html__( 'Add New Project', 'pixtypes' ), |
| 540 |
'edit_item' => esc_html__( 'Edit Project', 'pixtypes' ), |
| 541 |
'new_item' => esc_html__( 'New Project', 'pixtypes' ), |
| 542 |
'all_items' => esc_html__( 'All Projects', 'pixtypes' ), |
| 543 |
'view_item' => esc_html__( 'View Project', 'pixtypes' ), |
| 544 |
'search_items' => esc_html__( 'Search Projects', 'pixtypes' ), |
| 545 |
'not_found' => esc_html__( 'No Project found', 'pixtypes' ), |
| 546 |
'not_found_in_trash' => esc_html__( 'No Project found in Trash', 'pixtypes' ), |
| 547 |
'menu_name' => esc_html__( 'Projects', 'pixtypes' ), |
| 548 |
), |
| 549 |
'public' => true, |
| 550 |
'rewrite' => array( |
| 551 |
'slug' => 'portfolio', |
| 552 |
'with_front' => false, |
| 553 |
), |
| 554 |
'has_archive' => 'portfolio-archive', |
| 555 |
'menu_icon' => 'dashicons-portfolio', |
| 556 |
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes', 'excerpt' ), |
| 557 |
'yarpp_support' => true, |
| 558 |
), |
| 559 |
'pix_gallery' => array( |
| 560 |
'labels' => array( |
| 561 |
'name' => esc_html__( 'Gallery', 'pixtypes' ), |
| 562 |
'singular_name' => esc_html__( 'Gallery', 'pixtypes' ), |
| 563 |
'add_new' => esc_html__( 'Add New', 'pixtypes' ), |
| 564 |
'add_new_item' => esc_html__( 'Add New Gallery', 'pixtypes' ), |
| 565 |
'edit_item' => esc_html__( 'Edit Gallery', 'pixtypes' ), |
| 566 |
'new_item' => esc_html__( 'New Gallery', 'pixtypes' ), |
| 567 |
'all_items' => esc_html__( 'All Galleries', 'pixtypes' ), |
| 568 |
'view_item' => esc_html__( 'View Gallery', 'pixtypes' ), |
| 569 |
'search_items' => esc_html__( 'Search Galleries', 'pixtypes' ), |
| 570 |
'not_found' => esc_html__( 'No Gallery found', 'pixtypes' ), |
| 571 |
'not_found_in_trash' => esc_html__( 'No Gallery found in Trash', 'pixtypes' ), |
| 572 |
'menu_name' => esc_html__( 'Galleries', 'pixtypes' ), |
| 573 |
), |
| 574 |
'public' => true, |
| 575 |
'rewrite' => array( |
| 576 |
'slug' => 'galleries', |
| 577 |
'with_front' => false, |
| 578 |
), |
| 579 |
'has_archive' => 'galleries-archive', |
| 580 |
'menu_position' => null, |
| 581 |
'menu_icon' => 'dashicons-format-gallery', |
| 582 |
'supports' => array( 'title', 'thumbnail', 'page-attributes', 'excerpt' ), |
| 583 |
'yarpp_support' => true, |
| 584 |
), |
| 585 |
); |
| 586 |
|
| 587 |
/** TAXONOMIES **/ |
| 588 |
$types_options[ $theme_key ]['taxonomies'] = array( |
| 589 |
'pix_portfolio_categories' => array( |
| 590 |
'hierarchical' => true, |
| 591 |
'labels' => array( |
| 592 |
'name' => esc_html__( 'Portfolio Categories', 'pixtypes' ), |
| 593 |
'singular_name' => esc_html__( 'Portfolio Category', 'pixtypes' ), |
| 594 |
'search_items' => esc_html__( 'Search Portfolio Category', 'pixtypes' ), |
| 595 |
'all_items' => esc_html__( 'All Portfolio Categories', 'pixtypes' ), |
| 596 |
'parent_item' => esc_html__( 'Parent Portfolio Category', 'pixtypes' ), |
| 597 |
'parent_item_colon' => esc_html__( 'Parent Portfolio Category: ', 'pixtypes' ), |
| 598 |
'edit_item' => esc_html__( 'Edit Portfolio Category', 'pixtypes' ), |
| 599 |
'update_item' => esc_html__( 'Update Portfolio Category', 'pixtypes' ), |
| 600 |
'add_new_item' => esc_html__( 'Add New Portfolio Category', 'pixtypes' ), |
| 601 |
'new_item_name' => esc_html__( 'New Portfolio Category Name', 'pixtypes' ), |
| 602 |
'menu_name' => esc_html__( 'Portfolio Categories', 'pixtypes' ), |
| 603 |
), |
| 604 |
'show_admin_column' => true, |
| 605 |
'rewrite' => array( 'slug' => 'portfolio-category', 'with_front' => false ), |
| 606 |
'sort' => true, |
| 607 |
'post_types' => array( 'pix_portfolio' ) |
| 608 |
), |
| 609 |
'pix_gallery_categories' => array( |
| 610 |
'hierarchical' => true, |
| 611 |
'labels' => array( |
| 612 |
'name' => esc_html__( 'Gallery Categories', 'pixtypes' ), |
| 613 |
'singular_name' => esc_html__( 'Gallery Category', 'pixtypes' ), |
| 614 |
'search_items' => esc_html__( 'Search Gallery Category', 'pixtypes' ), |
| 615 |
'all_items' => esc_html__( 'All Gallery Categories', 'pixtypes' ), |
| 616 |
'parent_item' => esc_html__( 'Parent Gallery Category', 'pixtypes' ), |
| 617 |
'parent_item_colon' => esc_html__( 'Parent Gallery Category: ', 'pixtypes' ), |
| 618 |
'edit_item' => esc_html__( 'Edit Gallery Category', 'pixtypes' ), |
| 619 |
'update_item' => esc_html__( 'Update Gallery Category', 'pixtypes' ), |
| 620 |
'add_new_item' => esc_html__( 'Add New Gallery Category', 'pixtypes' ), |
| 621 |
'new_item_name' => esc_html__( 'New Gallery Category Name', 'pixtypes' ), |
| 622 |
'menu_name' => esc_html__( 'Gallery Categories', 'pixtypes' ), |
| 623 |
), |
| 624 |
'show_admin_column' => true, |
| 625 |
'rewrite' => array( 'slug' => 'gallery-category', 'with_front' => false ), |
| 626 |
'sort' => true, |
| 627 |
'post_types' => array( 'pix_gallery' ) |
| 628 |
), |
| 629 |
); |
| 630 |
|
| 631 |
/** METABOXES **/ |
| 632 |
$types_options[ $theme_key ]['metaboxes'] = array( |
| 633 |
'pix_portfolio' => array( |
| 634 |
'id' => 'portfolio_gallery', |
| 635 |
'title' => esc_html__( 'Gallery', 'pixtypes' ), |
| 636 |
'pages' => array( 'pix_portfolio' ), // Post type |
| 637 |
'context' => 'normal', |
| 638 |
'priority' => 'high', |
| 639 |
'show_names' => true, // Show field names on the left |
| 640 |
'fields' => array( |
| 641 |
array( |
| 642 |
'name' => esc_html__( 'Images', 'pixtypes' ), |
| 643 |
'id' => 'pix_portfolio_gallery', |
| 644 |
'type' => 'gallery', |
| 645 |
) |
| 646 |
) |
| 647 |
), |
| 648 |
'pix_gallery' => array( |
| 649 |
'id' => 'pix_gallery', |
| 650 |
'title' => esc_html__( 'Gallery', 'pixtypes' ), |
| 651 |
'pages' => array( 'pix_gallery' ), // Post type |
| 652 |
'context' => 'normal', |
| 653 |
'priority' => 'high', |
| 654 |
'show_names' => true, // Show field names on the left |
| 655 |
'fields' => array( |
| 656 |
array( |
| 657 |
'name' => esc_html__( 'Images', 'pixtypes' ), |
| 658 |
'id' => 'pix_main_gallery', |
| 659 |
'type' => 'gallery', |
| 660 |
) |
| 661 |
) |
| 662 |
) |
| 663 |
); |
| 664 |
|
| 665 |
return $types_options; |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Ajax callback for cleaning up the settings for a theme |
| 670 |
*/ |
| 671 |
function ajax_unset_pixtypes() { |
| 672 |
$result = array( 'success' => false, 'msg' => 'Incorrect nonce' ); |
| 673 |
|
| 674 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 675 |
wp_send_json_error( 'Unauthorized' ); |
| 676 |
} |
| 677 |
|
| 678 |
$ajax_nonce = isset( $_POST['_ajax_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ) : ''; |
| 679 |
if ( ! wp_verify_nonce( $ajax_nonce, 'unset_pixtype' ) ) { |
| 680 |
echo wp_json_encode( $result ); |
| 681 |
die(); |
| 682 |
} |
| 683 |
|
| 684 |
if ( isset( $_POST['theme_slug'] ) ) { |
| 685 |
$key = sanitize_key( wp_unslash( $_POST['theme_slug'] ) ); |
| 686 |
$options = get_option( 'pixtypes_settings' ); |
| 687 |
if ( isset( $options['themes'][ $key ] ) ) { |
| 688 |
unset( $options['themes'][ $key ] ); |
| 689 |
update_option( 'pixtypes_settings', $options ); |
| 690 |
$result['msg'] = 'Settings for ' . esc_html( ucfirst( $key ) ) . ' have been cleaned up!'; |
| 691 |
$result['success'] = true; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
echo wp_json_encode( $result ); |
| 696 |
exit; |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* On every wpgrade themes update we need to reconvert theme options into plugin options |
| 701 |
*/ |
| 702 |
function theme_version_check() { |
| 703 |
if ( class_exists( 'wpgrade' ) ) { |
| 704 |
// Each theme should have it's pixtypes config theme version saved |
| 705 |
$options = get_option( 'pixtypes_settings' ); |
| 706 |
|
| 707 |
// Make sure that we fix things just in case |
| 708 |
if ( ! isset( $options['wpgrade_theme_version'] ) ) { |
| 709 |
$options['wpgrade_theme_version'] = '0.0.1'; |
| 710 |
} |
| 711 |
|
| 712 |
if ( version_compare( wpgrade::themeversion(), $options['wpgrade_theme_version'], '!=' ) ) { |
| 713 |
// the plugin will copy these options into it's own field |
| 714 |
self::activate( false ); |
| 715 |
// end finally merge user's settings with the theme ones |
| 716 |
save_pixtypes_settings( $options ); |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
function get_plugin_version() { |
| 722 |
return $this->version; |
| 723 |
} |
| 724 |
} |
| 725 |
|