| 1 |
<?php |
| 2 |
/** |
| 3 |
* SlideshowSEPluginPostType creates a post type specifically designed for |
| 4 |
* slideshows and their individual settings |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @author: Stefan Boonstra |
| 8 |
*/ |
| 9 |
class SlideshowSEPluginPostType |
| 10 |
{ |
| 11 |
/** @var string $postType */ |
| 12 |
static $postType = 'slideshow'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize Slideshow post type. |
| 16 |
* Called on load of plugin |
| 17 |
* |
| 18 |
* @since 1.3.0 |
| 19 |
*/ |
| 20 |
static function init() |
| 21 |
{ |
| 22 |
add_action('init' , array(__CLASS__, 'registerSlideshowPostType')); |
| 23 |
add_action('save_post' , array('SlideshowSEPluginSlideshowSettingsHandler', 'save')); |
| 24 |
add_action('admin_enqueue_scripts', array('SlideshowSEPluginSlideInserter', 'localizeScript'), 11); |
| 25 |
|
| 26 |
add_action('admin_action_slideshow_jquery_image_gallery_duplicate_slideshow', array(__CLASS__, 'duplicateSlideshow'), 11); |
| 27 |
|
| 28 |
add_filter('post_updated_messages', array(__CLASS__, 'alterSlideshowMessages')); |
| 29 |
add_filter('post_row_actions' , array(__CLASS__, 'duplicateSlideshowActionLink'), 10, 2); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Registers new post type slideshow |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
*/ |
| 37 |
static function registerSlideshowPostType() |
| 38 |
{ |
| 39 |
global $wp_version; |
| 40 |
|
| 41 |
register_post_type( |
| 42 |
self::$postType, |
| 43 |
array( |
| 44 |
'labels' => array( |
| 45 |
'name' => __('Slideshows', 'slideshow-se'), |
| 46 |
'singular_name' => __('Slideshow', 'slideshow-se'), |
| 47 |
'add_new_item' => __('Add New Slideshow', 'slideshow-se'), |
| 48 |
'edit_item' => __('Edit slideshow', 'slideshow-se'), |
| 49 |
'new_item' => __('New slideshow', 'slideshow-se'), |
| 50 |
'view_item' => __('View slideshow', 'slideshow-se'), |
| 51 |
'search_items' => __('Search slideshows', 'slideshow-se'), |
| 52 |
'not_found' => __('No slideshows found', 'slideshow-se'), |
| 53 |
'not_found_in_trash' => __('No slideshows found', 'slideshow-se') |
| 54 |
), |
| 55 |
'public' => false, |
| 56 |
'publicly_queryable' => false, |
| 57 |
'show_ui' => true, |
| 58 |
'show_in_menu' => true, |
| 59 |
'query_var' => true, |
| 60 |
'rewrite' => true, |
| 61 |
'capability_type' => 'post', |
| 62 |
'capabilities' => array( |
| 63 |
'edit_post' => SlideshowSEPluginGeneralSettings::$capabilities['editSlideshows'], |
| 64 |
'read_post' => SlideshowSEPluginGeneralSettings::$capabilities['addSlideshows'], |
| 65 |
'delete_post' => SlideshowSEPluginGeneralSettings::$capabilities['deleteSlideshows'], |
| 66 |
'edit_posts' => SlideshowSEPluginGeneralSettings::$capabilities['editSlideshows'], |
| 67 |
'edit_others_posts' => SlideshowSEPluginGeneralSettings::$capabilities['editSlideshows'], |
| 68 |
'publish_posts' => SlideshowSEPluginGeneralSettings::$capabilities['addSlideshows'], |
| 69 |
'read_private_posts' => SlideshowSEPluginGeneralSettings::$capabilities['editSlideshows'], |
| 70 |
|
| 71 |
'read' => SlideshowSEPluginGeneralSettings::$capabilities['addSlideshows'], |
| 72 |
'delete_posts' => SlideshowSEPluginGeneralSettings::$capabilities['deleteSlideshows'], |
| 73 |
'delete_private_posts' => SlideshowSEPluginGeneralSettings::$capabilities['deleteSlideshows'], |
| 74 |
'delete_published_posts' => SlideshowSEPluginGeneralSettings::$capabilities['deleteSlideshows'], |
| 75 |
'delete_others_posts' => SlideshowSEPluginGeneralSettings::$capabilities['deleteSlideshows'], |
| 76 |
'edit_private_posts' => SlideshowSEPluginGeneralSettings::$capabilities['editSlideshows'], |
| 77 |
'edit_published_posts' => SlideshowSEPluginGeneralSettings::$capabilities['editSlideshows'], |
| 78 |
), |
| 79 |
'has_archive' => true, |
| 80 |
'hierarchical' => false, |
| 81 |
'menu_position' => null, |
| 82 |
'menu_icon' => version_compare($wp_version, '3.8', '<') ? SlideshowSEPluginMain::getPluginUrl() . '/images/' . __CLASS__ . '/adminIcon.png' : 'dashicons-format-gallery', |
| 83 |
'show_in_rest' => true, // added for Wordpress 5 REST API v2 support |
| 84 |
'rest_base' => 'slideshows', // added for Wordpress 5 REST API v2 support |
| 85 |
'supports' => array('title'), |
| 86 |
'register_meta_box_cb' => array(__CLASS__, 'registerMetaBoxes') |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Adds custom meta boxes to slideshow post type. |
| 93 |
* |
| 94 |
* @since 1.0.0 |
| 95 |
*/ |
| 96 |
static function registerMetaBoxes() |
| 97 |
{ |
| 98 |
add_meta_box( |
| 99 |
'information', |
| 100 |
__('Information', 'slideshow-se'), |
| 101 |
array(__CLASS__, 'informationMetaBox'), |
| 102 |
self::$postType, |
| 103 |
'normal', |
| 104 |
'high' |
| 105 |
); |
| 106 |
|
| 107 |
add_meta_box( |
| 108 |
'slides-list', |
| 109 |
__('Slides List', 'slideshow-se'), |
| 110 |
array(__CLASS__, 'slidesMetaBox'), |
| 111 |
self::$postType, |
| 112 |
'side', |
| 113 |
'default' |
| 114 |
); |
| 115 |
|
| 116 |
add_meta_box( |
| 117 |
'style', |
| 118 |
__('Slideshow Style', 'slideshow-se'), |
| 119 |
array(__CLASS__, 'styleMetaBox'), |
| 120 |
self::$postType, |
| 121 |
'normal', |
| 122 |
'low' |
| 123 |
); |
| 124 |
|
| 125 |
add_meta_box( |
| 126 |
'settings', |
| 127 |
__('Slideshow Settings', 'slideshow-se'), |
| 128 |
array(__CLASS__, 'settingsMetaBox'), |
| 129 |
self::$postType, |
| 130 |
'normal', |
| 131 |
'low' |
| 132 |
); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Changes the "Post published/updated" message to a "Slideshow created/updated" message without the link to a |
| 138 |
* frontend page. |
| 139 |
* |
| 140 |
* @since 2.2.20 |
| 141 |
* @param mixed $messages |
| 142 |
* @return mixed $messages |
| 143 |
*/ |
| 144 |
static function alterSlideshowMessages($messages) |
| 145 |
{ |
| 146 |
if (!function_exists('get_current_screen')) |
| 147 |
{ |
| 148 |
return $messages; |
| 149 |
} |
| 150 |
|
| 151 |
$currentScreen = get_current_screen(); |
| 152 |
|
| 153 |
// Return when not on a slideshow edit page |
| 154 |
if ($currentScreen->post_type != SlideshowSEPluginPostType::$postType) |
| 155 |
{ |
| 156 |
return $messages; |
| 157 |
} |
| 158 |
|
| 159 |
$messageID = filter_input(INPUT_GET, 'message', FILTER_VALIDATE_INT); |
| 160 |
|
| 161 |
if (!$messageID) |
| 162 |
{ |
| 163 |
return $messages; |
| 164 |
} |
| 165 |
|
| 166 |
switch ($messageID) |
| 167 |
{ |
| 168 |
case 6: |
| 169 |
$messages[$currentScreen->base][$messageID] = __('Slideshow created', 'slideshow-se'); |
| 170 |
break; |
| 171 |
|
| 172 |
default: |
| 173 |
$messages[$currentScreen->base][$messageID] = __('Slideshow updated', 'slideshow-se'); |
| 174 |
} |
| 175 |
|
| 176 |
return $messages; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Shows some information about this slideshow |
| 181 |
* |
| 182 |
* @since 1.0.0 |
| 183 |
*/ |
| 184 |
static function informationMetaBox() |
| 185 |
{ |
| 186 |
global $post; |
| 187 |
|
| 188 |
$data = new stdClass(); |
| 189 |
$data->snippet = htmlentities(sprintf('<?php do_action(\'slideshow_deploy\', \'%s\'); ?>', $post->ID)); |
| 190 |
$data->shortCode = htmlentities(sprintf('[' . SlideshowSEPluginShortcode::$shortCode . ' id=\'%s\']', $post->ID)); |
| 191 |
$data->postTitle = htmlentities(sprintf('%s', $post->post_title)); |
| 192 |
|
| 193 |
SlideshowSEPluginMain::outputView(__CLASS__ . DIRECTORY_SEPARATOR . 'information.php', $data); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Shows slides currently in slideshow |
| 198 |
* |
| 199 |
* @since 1.0.0 |
| 200 |
*/ |
| 201 |
static function slidesMetaBox() |
| 202 |
{ |
| 203 |
global $post; |
| 204 |
|
| 205 |
$data = new stdClass(); |
| 206 |
$data->slides = SlideshowSEPluginSlideshowSettingsHandler::getSlides($post->ID); |
| 207 |
|
| 208 |
SlideshowSEPluginMain::outputView(__CLASS__ . DIRECTORY_SEPARATOR . 'slides.php', $data); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Shows style used for slideshow |
| 213 |
* |
| 214 |
* @since 1.3.0 |
| 215 |
*/ |
| 216 |
static function styleMetaBox() |
| 217 |
{ |
| 218 |
global $post; |
| 219 |
|
| 220 |
$data = new stdClass(); |
| 221 |
$data->settings = SlideshowSEPluginSlideshowSettingsHandler::getStyleSettings($post->ID, true); |
| 222 |
|
| 223 |
SlideshowSEPluginMain::outputView(__CLASS__ . DIRECTORY_SEPARATOR . 'style-settings.php', $data); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Shows settings for particular slideshow |
| 228 |
* |
| 229 |
* @since 1.0.0 |
| 230 |
*/ |
| 231 |
static function settingsMetaBox() |
| 232 |
{ |
| 233 |
global $post; |
| 234 |
|
| 235 |
// Nonce |
| 236 |
wp_nonce_field(SlideshowSEPluginSlideshowSettingsHandler::$nonceAction, SlideshowSEPluginSlideshowSettingsHandler::$nonceName); |
| 237 |
|
| 238 |
$data = new stdClass(); |
| 239 |
$data->settings = SlideshowSEPluginSlideshowSettingsHandler::getSettings($post->ID, true); |
| 240 |
|
| 241 |
SlideshowSEPluginMain::outputView(__CLASS__ . DIRECTORY_SEPARATOR . 'settings.php', $data); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Hooked on the post_row_actions filter, adds a "duplicate" action to each slideshow on the slideshow's overview |
| 246 |
* page. |
| 247 |
* |
| 248 |
* @param array $actions |
| 249 |
* @param WP_Post $post |
| 250 |
* @return array $actions |
| 251 |
*/ |
| 252 |
static function duplicateSlideshowActionLink($actions, $post) |
| 253 |
{ |
| 254 |
if (current_user_can('slideshow-jquery-image-gallery-add-slideshows') && |
| 255 |
$post->post_type === self::$postType) |
| 256 |
{ |
| 257 |
$url = add_query_arg(array( |
| 258 |
'action' => 'slideshow_jquery_image_gallery_duplicate_slideshow', |
| 259 |
'post' => $post->ID, |
| 260 |
)); |
| 261 |
|
| 262 |
$actions['duplicate'] = '<a href="' . wp_nonce_url($url, 'duplicate-slideshow_' . $post->ID, 'nonce') . '">' . __('Duplicate', 'slideshow-se') . '</a>'; |
| 263 |
} |
| 264 |
|
| 265 |
return $actions; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Checks if a "duplicate" slideshow action was performed and whether or not the current user has the permission to |
| 270 |
* perform this action at all. |
| 271 |
*/ |
| 272 |
static function duplicateSlideshow() |
| 273 |
{ |
| 274 |
$postID = filter_input(INPUT_GET, 'post' , FILTER_VALIDATE_INT); |
| 275 |
$nonce = filter_input(INPUT_GET, 'nonce' , FILTER_SANITIZE_STRING); |
| 276 |
$postType = filter_input(INPUT_GET, 'post_type', FILTER_SANITIZE_STRING); |
| 277 |
$errorRedirectURL = remove_query_arg(array('action', 'post', 'nonce')); |
| 278 |
|
| 279 |
// Check if nonce is correct and user has the correct privileges |
| 280 |
if (!wp_verify_nonce($nonce, 'duplicate-slideshow_' . $postID) || |
| 281 |
!current_user_can('slideshow-jquery-image-gallery-add-slideshows') || |
| 282 |
$postType !== self::$postType) |
| 283 |
{ |
| 284 |
wp_redirect($errorRedirectURL); |
| 285 |
|
| 286 |
die(); |
| 287 |
} |
| 288 |
|
| 289 |
$post = get_post($postID); |
| 290 |
|
| 291 |
// Check if the post was retrieved successfully |
| 292 |
if (!$post instanceof WP_Post || |
| 293 |
$post->post_type !== self::$postType) |
| 294 |
{ |
| 295 |
wp_redirect($errorRedirectURL); |
| 296 |
|
| 297 |
die(); |
| 298 |
} |
| 299 |
|
| 300 |
$current_user = wp_get_current_user(); |
| 301 |
|
| 302 |
// Create post duplicate |
| 303 |
$newPostID = wp_insert_post(array( |
| 304 |
'comment_status' => $post->comment_status, |
| 305 |
'ping_status' => $post->ping_status, |
| 306 |
'post_author' => $current_user->ID, |
| 307 |
'post_content' => $post->post_content, |
| 308 |
'post_excerpt' => $post->post_excerpt, |
| 309 |
'post_name' => $post->post_name, |
| 310 |
'post_parent' => $post->post_parent, |
| 311 |
'post_password' => $post->post_password, |
| 312 |
'post_status' => 'draft', |
| 313 |
'post_title' => $post->post_title . (strlen($post->post_title) > 0 ? ' - ' : '') . __('Copy', 'slideshow-se'), |
| 314 |
'post_type' => $post->post_type, |
| 315 |
'to_ping' => $post->to_ping, |
| 316 |
'menu_order' => $post->menu_order, |
| 317 |
)); |
| 318 |
|
| 319 |
if (is_wp_error($newPostID)) |
| 320 |
{ |
| 321 |
wp_redirect($errorRedirectURL); |
| 322 |
|
| 323 |
die(); |
| 324 |
} |
| 325 |
|
| 326 |
// Get all taxonomies |
| 327 |
$taxonomies = get_object_taxonomies($post->post_type); |
| 328 |
|
| 329 |
// Add taxonomies to new post |
| 330 |
foreach ($taxonomies as $taxonomy) |
| 331 |
{ |
| 332 |
$postTerms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'slugs')); |
| 333 |
|
| 334 |
wp_set_object_terms($newPostID, $postTerms, $taxonomy, false); |
| 335 |
} |
| 336 |
|
| 337 |
// Get all post meta |
| 338 |
$postMetaRecords = get_post_meta($post->ID); |
| 339 |
|
| 340 |
// Add post meta records to new post |
| 341 |
foreach ($postMetaRecords as $postMetaKey => $postMetaValues) |
| 342 |
{ |
| 343 |
foreach ($postMetaValues as $postMetaValue) |
| 344 |
{ |
| 345 |
update_post_meta($newPostID, $postMetaKey, maybe_unserialize($postMetaValue)); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
wp_redirect(admin_url('post.php?action=edit&post=' . $newPostID)); |
| 350 |
|
| 351 |
die(); |
| 352 |
} |
| 353 |
} |