| @@ -1,105 +1,338 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | - /** | |
| 4 | - * Php Snippets Type | |
| 5 | - * Declaration for custom post type of Php code snippets | |
| 6 | - * @link http://codex.wordpress.org/Post_Types | |
| 7 | - */ | |
| 8 | - class WSC_TasksItemType extends Wbcr_FactoryTypes404_Type { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * Custom post name. | |
| 12 | - * @var string | |
| 13 | - */ | |
| 14 | - //public $name = 'wbcr-scrapes'; | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * Template that defines a set of type options. | |
| 18 | - * Allowed values: public, private, internal. | |
| 19 | - * @var string | |
| 20 | - */ | |
| 21 | - public $template = 'private'; | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * Capabilities for roles that have access to manage the type. | |
| 25 | - * @link http://codex.wordpress.org/Roles_and_Capabilities | |
| 26 | - * @var array | |
| 27 | - */ | |
| 28 | - public $capabilities = array('administrator'); | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * @param Wbcr_Factory404_Plugin $plugin | |
| 32 | - */ | |
| 33 | - function __construct(Wbcr_Factory404_Plugin $plugin) | |
| 34 | - { | |
| 35 | - $this->name = WINP_SNIPPETS_POST_TYPE; | |
| 36 | - $this->plural_title = __('PHP snippets', 'insert-php'); | |
| 37 | - $this->singular_title = __('PHP snippets', 'insert-php'); | |
| 38 | - | |
| 39 | - parent::__construct($plugin); | |
| 40 | - } | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Type configurator. | |
| 44 | - */ | |
| 45 | - public function configure() | |
| 46 | - { | |
| 47 | - | |
| 48 | - $plural_name = $this->plural_title; | |
| 49 | - $singular_name = $this->singular_title; | |
| 50 | - | |
| 51 | - $labels = array( | |
| 52 | - 'singular_name' => $this->singular_title, | |
| 53 | - 'name' => $this->plural_title, | |
| 54 | - 'all_items' => sprintf(__('Snippets', 'insert-php'), $plural_name), | |
| 55 | - 'add_new' => sprintf(__('+ Add snippet', 'insert-php'), $singular_name), | |
| 56 | - 'add_new_item' => sprintf(__('Add new', 'insert-php'), $singular_name), | |
| 57 | - 'edit' => sprintf(__('Edit', 'insert-php')), | |
| 58 | - 'edit_item' => sprintf(__('Edit snippet', 'insert-php'), $singular_name), | |
| 59 | - 'new_item' => sprintf(__('New snippet', 'insert-php'), $singular_name), | |
| 60 | - 'view' => sprintf(__('View', 'factory')), | |
| 61 | - 'view_item' => sprintf(__('View snippet', 'insert-php'), $singular_name), | |
| 62 | - 'search_items' => sprintf(__('Search snippets', 'insert-php'), $plural_name), | |
| 63 | - 'not_found' => sprintf(__('Snippet is not found', 'insert-php'), $plural_name), | |
| 64 | - 'not_found_in_trash' => sprintf(__('Snippt is not found in trash', 'insert-php'), $plural_name), | |
| 65 | - 'parent' => sprintf(__('Parent snippet', 'insert-php'), $plural_name) | |
| 66 | - ); | |
| 67 | - | |
| 68 | - $this->options['labels'] = apply_filters('wbcr_inp_items_lables', $labels); | |
| 69 | - | |
| 70 | - /** | |
| 71 | - * Menu | |
| 72 | - */ | |
| 73 | - | |
| 74 | - $this->menu->icon = WINP_PLUGIN_URL . '/admin/assets/img/menu-icon.png'; | |
| 75 | - | |
| 76 | - /** | |
| 77 | - * View table | |
| 78 | - */ | |
| 79 | - | |
| 80 | - $this->view_table = 'WINP_SnippetsViewTable'; | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Scripts & styles | |
| 84 | - */ | |
| 85 | - | |
| 86 | - $this->scripts->request(array('jquery', 'jquery-effects-highlight', 'jquery-effects-slide')); | |
| 87 | - | |
| 88 | - $this->scripts->request(array( | |
| 89 | - 'control.checkbox', | |
| 90 | - 'control.dropdown', | |
| 91 | - 'control.list', | |
| 92 | - ), 'bootstrap'); | |
| 93 | - | |
| 94 | - $this->styles->request(array( | |
| 95 | - 'bootstrap.core', | |
| 96 | - 'bootstrap.form-group', | |
| 97 | - 'bootstrap.form-metabox', | |
| 98 | - 'bootstrap.wp-editor', | |
| 99 | - 'bootstrap.separator', | |
| 100 | - 'control.checkbox', | |
| 101 | - 'control.dropdown', | |
| 102 | - 'control.list', | |
| 103 | - ), 'bootstrap'); | |
| 104 | - } | |
| 105 | - } | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Php Snippets Type | |
| 4 | + * Declaration for custom post type of Php code snippets | |
| 5 | + * | |
| 6 | + * @package Woody_Code_Snippets | |
| 7 | + * @link http://codex.wordpress.org/Post_Types | |
| 8 | + */ | |
| 9 | + | |
| 10 | +// Exit if accessed directly. | |
| 11 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit; | |
| 13 | +} | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * Class WINP_SnippetsType | |
| 17 | + * | |
| 18 | + * Handles registration and configuration of the snippets custom post type. | |
| 19 | + */ | |
| 20 | +class WINP_SnippetsType { | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Post type name. | |
| 24 | + * | |
| 25 | + * @var string | |
| 26 | + */ | |
| 27 | + private $name; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Plural title. | |
| 31 | + * | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + private $plural_title; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Singular title. | |
| 38 | + * | |
| 39 | + * @var string | |
| 40 | + */ | |
| 41 | + private $singular_title; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Menu icon URL. | |
| 45 | + * | |
| 46 | + * @var string | |
| 47 | + */ | |
| 48 | + private $menu_icon; | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Post type capabilities. | |
| 52 | + * | |
| 53 | + * @var string[] | |
| 54 | + */ | |
| 55 | + private $capabilities = [ 'administrator' ]; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Constructor. | |
| 59 | + */ | |
| 60 | + public function __construct() { | |
| 61 | + $this->name = WINP_SNIPPETS_POST_TYPE; | |
| 62 | + $this->plural_title = __( 'Woody snippets', 'insert-php' ); | |
| 63 | + $this->singular_title = __( 'Woody snippets', 'insert-php' ); | |
| 64 | + $this->menu_icon = WINP_PLUGIN_URL . '/admin/assets/img/menu-icon-4.png'; | |
| 65 | + | |
| 66 | + // Initialize view table. | |
| 67 | + new WINP_SnippetsViewTable(); | |
| 68 | + | |
| 69 | + // Register hooks. | |
| 70 | + add_action( 'init', [ $this, 'register' ] ); | |
| 71 | + add_action( 'admin_head', [ $this, 'print_menu_styles' ] ); | |
| 72 | + add_action( 'admin_head', [ $this, 'print_menu_icon_styles' ] ); | |
| 73 | + add_filter( 'post_updated_messages', [ $this, 'updated_messages' ] ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Register the custom post type. | |
| 78 | + * | |
| 79 | + * @return void | |
| 80 | + */ | |
| 81 | + public function register() { | |
| 82 | + $labels = $this->get_labels(); | |
| 83 | + $args = $this->get_post_type_args( $labels ); | |
| 84 | + | |
| 85 | + register_post_type( $this->name, $args ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Get post type labels. | |
| 90 | + * | |
| 91 | + * @return string[] Post type labels. | |
| 92 | + */ | |
| 93 | + private function get_labels() { | |
| 94 | + $labels = [ | |
| 95 | + 'name' => $this->plural_title, | |
| 96 | + 'singular_name' => $this->singular_title, | |
| 97 | + 'all_items' => __( 'Snippets', 'insert-php' ), | |
| 98 | + 'add_new' => __( '+ Add snippet', 'insert-php' ), | |
| 99 | + 'add_new_item' => __( 'Add New Snippet', 'insert-php' ), | |
| 100 | + 'edit' => __( 'Edit', 'insert-php' ), | |
| 101 | + 'edit_item' => __( 'Edit snippet', 'insert-php' ), | |
| 102 | + 'new_item' => __( 'New snippet', 'insert-php' ), | |
| 103 | + 'view' => __( 'View Snippet', 'insert-php' ), | |
| 104 | + 'view_item' => __( 'View snippet', 'insert-php' ), | |
| 105 | + 'search_items' => __( 'Search snippets', 'insert-php' ), | |
| 106 | + 'not_found' => __( 'Snippet not found', 'insert-php' ), | |
| 107 | + 'not_found_in_trash' => __( 'Snippet not found in trash', 'insert-php' ), | |
| 108 | + 'parent' => __( 'Parent snippet', 'insert-php' ), | |
| 109 | + ]; | |
| 110 | + | |
| 111 | + return apply_filters( 'wbcr_inp_items_lables', $labels ); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Get post type registration arguments. | |
| 116 | + * | |
| 117 | + * @param string[] $labels Post type labels. | |
| 118 | + * @return array<string, mixed> Post type args. | |
| 119 | + */ | |
| 120 | + private function get_post_type_args( $labels ) { | |
| 121 | + $snippet_type = WINP_Helper::get_snippet_type(); | |
| 122 | + $supports = [ 'title', 'revisions' ]; | |
| 123 | + | |
| 124 | + // Add editor support for text and ad snippets. | |
| 125 | + if ( WINP_SNIPPET_TYPE_TEXT === $snippet_type || WINP_SNIPPET_TYPE_AD === $snippet_type ) { | |
| 126 | + $supports[] = 'editor'; | |
| 127 | + } | |
| 128 | + | |
| 129 | + $supports = apply_filters( 'wbcr_inp_items_supports', $supports ); | |
| 130 | + $can_export = WINP_Plugin::app()->get_api_object()->is_key(); | |
| 131 | + $menu_icon = $this->get_menu_icon_url(); | |
| 132 | + $capabilities = $this->get_capabilities(); | |
| 133 | + | |
| 134 | + $args = [ | |
| 135 | + 'labels' => $labels, | |
| 136 | + 'public' => false, | |
| 137 | + 'publicly_queryable' => false, | |
| 138 | + 'exclude_from_search' => true, | |
| 139 | + 'show_ui' => true, | |
| 140 | + 'show_in_menu' => true, | |
| 141 | + 'menu_position' => null, | |
| 142 | + 'menu_icon' => $menu_icon, | |
| 143 | + 'capability_type' => $this->name, | |
| 144 | + 'capabilities' => $capabilities, | |
| 145 | + 'hierarchical' => false, | |
| 146 | + 'supports' => $supports, | |
| 147 | + 'has_archive' => false, | |
| 148 | + 'rewrite' => false, | |
| 149 | + 'query_var' => true, | |
| 150 | + 'show_in_nav_menus' => false, | |
| 151 | + 'can_export' => $can_export, | |
| 152 | + ]; | |
| 153 | + | |
| 154 | + return $args; | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Get menu icon URL. | |
| 159 | + * | |
| 160 | + * @return string Menu icon URL. | |
| 161 | + */ | |
| 162 | + private function get_menu_icon_url() { | |
| 163 | + return $this->menu_icon; | |
| 164 | + } | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * Get custom capabilities map. | |
| 168 | + * | |
| 169 | + * @return array<string, string> Capabilities map. | |
| 170 | + */ | |
| 171 | + private function get_capabilities() { | |
| 172 | + return [ | |
| 173 | + 'edit_post' => 'edit_' . $this->name, | |
| 174 | + 'read_post' => 'read_' . $this->name, | |
| 175 | + 'delete_post' => 'delete_' . $this->name, | |
| 176 | + 'delete_posts' => 'delete_' . $this->name . 's', | |
| 177 | + 'edit_posts' => 'edit_' . $this->name . 's', | |
| 178 | + 'edit_others_posts' => 'edit_others_' . $this->name . 's', | |
| 179 | + 'publish_posts' => 'publish_' . $this->name . 's', | |
| 180 | + 'read_private_posts' => 'read_private_' . $this->name . 's', | |
| 181 | + 'create_posts' => 'edit_' . $this->name . 's', | |
| 182 | + ]; | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Print menu styles for Woody snippets. | |
| 187 | + * | |
| 188 | + * @return void | |
| 189 | + */ | |
| 190 | + public function print_menu_styles() { | |
| 191 | + ?> | |
| 192 | + <!-- Woody Code Snippets --> | |
| 193 | + <style> | |
| 194 | + #menu-posts-wbcr-snippets .wp-menu-open .wp-menu-name { | |
| 195 | + background: #242525; | |
| 196 | + } | |
| 197 | + </style> | |
| 198 | + <!-- /Woody Code Snippets --> | |
| 199 | + <?php | |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 203 | + * Print menu icon styles. | |
| 204 | + * | |
| 205 | + * @return void | |
| 206 | + */ | |
| 207 | + public function print_menu_icon_styles() { | |
| 208 | + if ( empty( $this->menu_icon ) ) { | |
| 209 | + return; | |
| 210 | + } | |
| 211 | + | |
| 212 | + $icon_url = $this->menu_icon; | |
| 213 | + $icon_url32 = str_replace( '.png', '-32.png', $icon_url ); | |
| 214 | + | |
| 215 | + ?> | |
| 216 | + <style type="text/css" media="screen"> | |
| 217 | + #menu-posts-<?php echo esc_attr( $this->name ); ?> .wp-menu-image { | |
| 218 | + background: url('<?php echo esc_url( $icon_url ); ?>') no-repeat 10px -30px !important; | |
| 219 | + overflow: hidden; | |
| 220 | + } | |
| 221 | + | |
| 222 | + #menu-posts-<?php echo esc_attr( $this->name ); ?> .wp-menu-image:before { | |
| 223 | + content: "" !important; | |
| 224 | + } | |
| 225 | + | |
| 226 | + #menu-posts-<?php echo esc_attr( $this->name ); ?>:hover .wp-menu-image, | |
| 227 | + #menu-posts-<?php echo esc_attr( $this->name ); ?>.wp-has-current-submenu .wp-menu-image { | |
| 228 | + background-position: 10px 2px !important; | |
| 229 | + } | |
| 230 | + | |
| 231 | + #icon-edit.icon32-posts-<?php echo esc_attr( $this->name ); ?> { | |
| 232 | + background: url('<?php echo esc_url( $icon_url32 ); ?>') no-repeat; | |
| 233 | + } | |
| 234 | + </style> | |
| 235 | + <?php | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Customize post update messages. | |
| 240 | + * | |
| 241 | + * @param array<string, array<int, string|false>> $messages Existing messages. | |
| 242 | + * @return array<string, array<int, string|false>> Modified messages. | |
| 243 | + */ | |
| 244 | + public function updated_messages( $messages ) { | |
| 245 | + global $post; | |
| 246 | + | |
| 247 | + if ( ! $post || $post->post_type !== $this->name ) { | |
| 248 | + return $messages; | |
| 249 | + } | |
| 250 | + | |
| 251 | + $singular = $this->singular_title; | |
| 252 | + $revision = WINP_HTTP::get( 'revision', 0, 'absint' ); | |
| 253 | + | |
| 254 | + $messages[ $this->name ] = [ | |
| 255 | + 0 => '', // Unused. Messages start at index 1. | |
| 256 | + 1 => $singular . ' ' . __( 'updated.', 'insert-php' ), | |
| 257 | + 2 => __( 'Custom field updated successfully.', 'insert-php' ), | |
| 258 | + 3 => __( 'Custom field deleted.', 'insert-php' ), | |
| 259 | + 4 => $singular . ' ' . __( 'updated.', 'insert-php' ), | |
| 260 | + 5 => ! empty( $revision ) | |
| 261 | + ? $singular . ' ' . sprintf( | |
| 262 | + /* translators: %s: revision title */ | |
| 263 | + __( 'restored to revision from %s', 'insert-php' ), | |
| 264 | + wp_post_revision_title( $revision, false ) | |
| 265 | + ) | |
| 266 | + : false, | |
| 267 | + 6 => $singular . ' ' . __( 'created.', 'insert-php' ), | |
| 268 | + 7 => $singular . ' ' . __( 'saved.', 'insert-php' ), | |
| 269 | + 8 => $singular . ' ' . __( 'submitted.', 'insert-php' ), | |
| 270 | + 9 => $singular . ' ' . sprintf( | |
| 271 | + /* translators: %s: scheduled date */ | |
| 272 | + __( 'scheduled for: %s', 'insert-php' ), | |
| 273 | + '<strong>' . date_i18n( __( 'M j, Y @ G:i', 'insert-php' ), strtotime( $post->post_date ) ) . '</strong>' | |
| 274 | + ), | |
| 275 | + 10 => $singular . ' ' . __( 'draft updated.', 'insert-php' ), | |
| 276 | + ]; | |
| 277 | + | |
| 278 | + return $messages; | |
| 279 | + } | |
| 280 | + | |
| 281 | + /** | |
| 282 | + * Add capabilities to administrator role. | |
| 283 | + * Called on plugin activation. | |
| 284 | + * | |
| 285 | + * @return void | |
| 286 | + */ | |
| 287 | + public function add_capabilities() { | |
| 288 | + if ( empty( $this->capabilities ) ) { | |
| 289 | + return; | |
| 290 | + } | |
| 291 | + | |
| 292 | + foreach ( $this->capabilities as $role_name ) { | |
| 293 | + $role = get_role( $role_name ); | |
| 294 | + if ( ! $role ) { | |
| 295 | + continue; | |
| 296 | + } | |
| 297 | + | |
| 298 | + $role->add_cap( 'edit_' . $this->name ); | |
| 299 | + $role->add_cap( 'read_' . $this->name ); | |
| 300 | + $role->add_cap( 'delete_' . $this->name ); | |
| 301 | + $role->add_cap( 'edit_' . $this->name . 's' ); | |
| 302 | + $role->add_cap( 'edit_others_' . $this->name . 's' ); | |
| 303 | + $role->add_cap( 'publish_' . $this->name . 's' ); | |
| 304 | + $role->add_cap( 'read_private_' . $this->name . 's' ); | |
| 305 | + } | |
| 306 | + } | |
| 307 | + | |
| 308 | + /** | |
| 309 | + * Remove capabilities from all roles. | |
| 310 | + * Called on plugin deactivation. | |
| 311 | + * | |
| 312 | + * @return void | |
| 313 | + */ | |
| 314 | + public function remove_capabilities() { | |
| 315 | + global $wp_roles; | |
| 316 | + | |
| 317 | + if ( ! isset( $wp_roles ) ) { | |
| 318 | + return; | |
| 319 | + } | |
| 320 | + | |
| 321 | + $all_roles = $wp_roles->roles; | |
| 322 | + | |
| 323 | + foreach ( $all_roles as $role_name => $role_info ) { | |
| 324 | + $role = get_role( $role_name ); | |
| 325 | + if ( ! $role ) { | |
| 326 | + continue; | |
| 327 | + } | |
| 328 | + | |
| 329 | + $role->remove_cap( 'edit_' . $this->name ); | |
| 330 | + $role->remove_cap( 'read_' . $this->name ); | |
| 331 | + $role->remove_cap( 'delete_' . $this->name ); | |
| 332 | + $role->remove_cap( 'edit_' . $this->name . 's' ); | |
| 333 | + $role->remove_cap( 'edit_others_' . $this->name . 's' ); | |
| 334 | + $role->remove_cap( 'publish_' . $this->name . 's' ); | |
| 335 | + $role->remove_cap( 'read_private_' . $this->name . 's' ); | |
| 336 | + } | |
| 337 | + } | |
| 338 | +} | |