| @@ -1,141 +1,338 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | 2 | /** |
| 4 | 3 | * Php Snippets Type |
| 5 | 4 | * Declaration for custom post type of Php code snippets |
| 6 | 5 | * |
| 7 | - * @link http://codex.wordpress.org/Post_Types | |
| 6 | + * @package Woody_Code_Snippets | |
| 7 | + * @link http://codex.wordpress.org/Post_Types | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -// Exit if accessed directly | |
| 10 | +// Exit if accessed directly. | |
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | 12 | exit; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | -class WINP_SnippetsType extends Wbcr_FactoryTypes412_Type { | |
| 15 | +/** | |
| 16 | + * Class WINP_SnippetsType | |
| 17 | + * | |
| 18 | + * Handles registration and configuration of the snippets custom post type. | |
| 19 | + */ | |
| 20 | +class WINP_SnippetsType { | |
| 16 | 21 | |
| 17 | 22 | /** |
| 18 | - * Custom post name. | |
| 23 | + * Post type name. | |
| 19 | 24 | * |
| 20 | 25 | * @var string |
| 21 | 26 | */ |
| 22 | - //public $name = 'wbcr-scrapes'; | |
| 27 | + private $name; | |
| 23 | 28 | |
| 24 | 29 | /** |
| 25 | - * Template that defines a set of type options. | |
| 26 | - * Allowed values: public, private, internal. | |
| 30 | + * Plural title. | |
| 27 | 31 | * |
| 28 | 32 | * @var string |
| 29 | 33 | */ |
| 30 | - public $template = 'private'; | |
| 34 | + private $plural_title; | |
| 31 | 35 | |
| 32 | 36 | /** |
| 33 | - * Capabilities for roles that have access to manage the type. | |
| 37 | + * Singular title. | |
| 34 | 38 | * |
| 35 | - * @link http://codex.wordpress.org/Roles_and_Capabilities | |
| 36 | - * @var array | |
| 39 | + * @var string | |
| 37 | 40 | */ |
| 38 | - public $capabilities = [ 'administrator' ]; | |
| 41 | + private $singular_title; | |
| 39 | 42 | |
| 40 | 43 | /** |
| 41 | - * @param Wbcr_Factory456_Plugin $plugin | |
| 44 | + * Menu icon URL. | |
| 45 | + * | |
| 46 | + * @var string | |
| 42 | 47 | */ |
| 43 | - function __construct( Wbcr_Factory456_Plugin $plugin ) { | |
| 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() { | |
| 44 | 61 | $this->name = WINP_SNIPPETS_POST_TYPE; |
| 45 | 62 | $this->plural_title = __( 'Woody snippets', 'insert-php' ); |
| 46 | 63 | $this->singular_title = __( 'Woody snippets', 'insert-php' ); |
| 64 | + $this->menu_icon = WINP_PLUGIN_URL . '/admin/assets/img/menu-icon-4.png'; | |
| 47 | 65 | |
| 48 | - parent::__construct( $plugin ); | |
| 66 | + // Initialize view table. | |
| 67 | + new WINP_SnippetsViewTable(); | |
| 49 | 68 | |
| 50 | - add_action( 'admin_head', [ $this, 'print_left_menu_styles' ] ); | |
| 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' ] ); | |
| 51 | 74 | } |
| 52 | 75 | |
| 53 | 76 | /** |
| 54 | - * Prints styles for Woody add snippets menu | |
| 77 | + * Register the custom post type. | |
| 78 | + * | |
| 79 | + * @return void | |
| 55 | 80 | */ |
| 56 | - public function print_left_menu_styles() { | |
| 57 | - ?> | |
| 58 | - <!-- Woody Code Snippets --> | |
| 59 | - <style> | |
| 60 | - #menu-posts-wbcr-snippets .wp-menu-open .wp-menu-name { | |
| 61 | - background: #242525; | |
| 62 | - } | |
| 63 | - </style> | |
| 64 | - <!-- /Woody Code Snippets --> | |
| 65 | - <?php | |
| 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 ); | |
| 66 | 86 | } |
| 67 | 87 | |
| 68 | 88 | /** |
| 69 | - * Type configurator. | |
| 89 | + * Get post type labels. | |
| 90 | + * | |
| 91 | + * @return string[] Post type labels. | |
| 70 | 92 | */ |
| 71 | - public function configure() { | |
| 72 | - $plural_name = $this->plural_title; | |
| 73 | - $singular_name = $this->singular_title; | |
| 74 | - | |
| 93 | + private function get_labels() { | |
| 75 | 94 | $labels = [ |
| 95 | + 'name' => $this->plural_title, | |
| 76 | 96 | 'singular_name' => $this->singular_title, |
| 77 | - 'name' => $this->plural_title, | |
| 78 | - 'all_items' => sprintf( __( 'Snippets', 'insert-php' ), $plural_name ), | |
| 79 | - 'add_new' => sprintf( __( '+ Add snippet', 'insert-php' ), $singular_name ), | |
| 80 | - 'add_new_item' => sprintf( __( 'Add new', 'insert-php' ), $singular_name ), | |
| 81 | - 'edit' => sprintf( __( 'Edit', 'insert-php' ) ), | |
| 82 | - 'edit_item' => sprintf( __( 'Edit snippet', 'insert-php' ), $singular_name ), | |
| 83 | - 'new_item' => sprintf( __( 'New snippet', 'insert-php' ), $singular_name ), | |
| 84 | - 'view' => sprintf( __( 'View', 'factory' ) ), | |
| 85 | - 'view_item' => sprintf( __( 'View snippet', 'insert-php' ), $singular_name ), | |
| 86 | - 'search_items' => sprintf( __( 'Search snippets', 'insert-php' ), $plural_name ), | |
| 87 | - 'not_found' => sprintf( __( 'Snippet is not found', 'insert-php' ), $plural_name ), | |
| 88 | - 'not_found_in_trash' => sprintf( __( 'Snippt is not found in trash', 'insert-php' ), $plural_name ), | |
| 89 | - 'parent' => sprintf( __( 'Parent snippet', 'insert-php' ), $plural_name ) | |
| 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' ), | |
| 90 | 109 | ]; |
| 91 | 110 | |
| 92 | - $this->options['labels'] = apply_filters( 'wbcr_inp_items_lables', $labels ); | |
| 93 | - $this->options['can_export'] = WINP_Plugin::app()->get_api_object()->is_key() ? true : false; | |
| 111 | + return apply_filters( 'wbcr_inp_items_lables', $labels ); | |
| 112 | + } | |
| 94 | 113 | |
| 95 | - $parameters = [ 'title', 'revisions' ]; | |
| 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 ) { | |
| 96 | 121 | $snippet_type = WINP_Helper::get_snippet_type(); |
| 97 | - if ( $snippet_type === WINP_SNIPPET_TYPE_TEXT || $snippet_type === WINP_SNIPPET_TYPE_AD ) { | |
| 98 | - $parameters[] = 'editor'; | |
| 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'; | |
| 99 | 127 | } |
| 100 | - $this->options['supports'] = apply_filters( 'wbcr_inp_items_supports', $parameters ); | |
| 101 | 128 | |
| 102 | - /** | |
| 103 | - * Menu | |
| 104 | - */ | |
| 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(); | |
| 105 | 133 | |
| 106 | - $this->menu->icon = WINP_PLUGIN_URL . '/admin/assets/img/menu-icon-4.png'; | |
| 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 | + ]; | |
| 107 | 153 | |
| 108 | - /** | |
| 109 | - * View table | |
| 110 | - */ | |
| 154 | + return $args; | |
| 155 | + } | |
| 111 | 156 | |
| 112 | - $this->view_table = 'WINP_SnippetsViewTable'; | |
| 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 | + } | |
| 113 | 165 | |
| 114 | - /** | |
| 115 | - * Scripts & styles | |
| 116 | - */ | |
| 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 | + } | |
| 117 | 184 | |
| 118 | - $this->scripts->request( [ 'jquery', 'jquery-effects-highlight', 'jquery-effects-slide' ] ); | |
| 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 | + } | |
| 119 | 201 | |
| 120 | - $this->scripts->request( [ | |
| 121 | - 'bootstrap.datepicker', | |
| 122 | - 'control.checkbox', | |
| 123 | - 'control.dropdown', | |
| 124 | - 'control.list', | |
| 125 | - 'bootstrap.tooltip', | |
| 126 | - 'bootstrap.modal', | |
| 127 | - ], 'bootstrap' ); | |
| 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 | + } | |
| 128 | 211 | |
| 129 | - $this->styles->request( [ | |
| 130 | - 'bootstrap.core', | |
| 131 | - 'bootstrap.datepicker', | |
| 132 | - 'bootstrap.form-group', | |
| 133 | - 'bootstrap.form-metabox', | |
| 134 | - 'bootstrap.wp-editor', | |
| 135 | - 'bootstrap.separator', | |
| 136 | - 'control.checkbox', | |
| 137 | - 'control.dropdown', | |
| 138 | - 'control.list', | |
| 139 | - ], 'bootstrap' ); | |
| 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 | |
| 140 | 236 | } |
| 141 | -} | |
| 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 | +} | |