PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.0
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.0
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
insert-php / admin / types / snippets-post-types.php

snippets-post-types.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.7.0, at admin/types/snippets-post-types.php

337 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
253 $messages[ $this->name ] = [
254 0 => '', // Unused. Messages start at index 1.
255 1 => $singular . ' ' . __( 'updated.', 'insert-php' ),
256 2 => __( 'Custom field updated successfully.', 'insert-php' ),
257 3 => __( 'Custom field deleted.', 'insert-php' ),
258 4 => $singular . ' ' . __( 'updated.', 'insert-php' ),
259 5 => isset( $_GET['revision'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
260 ? $singular . ' ' . sprintf(
261 /* translators: %s: revision title */
262 __( 'restored to revision from %s', 'insert-php' ),
263 wp_post_revision_title( (int) $_GET['revision'], false ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
264 )
265 : false,
266 6 => $singular . ' ' . __( 'created.', 'insert-php' ),
267 7 => $singular . ' ' . __( 'saved.', 'insert-php' ),
268 8 => $singular . ' ' . __( 'submitted.', 'insert-php' ),
269 9 => $singular . ' ' . sprintf(
270 /* translators: %s: scheduled date */
271 __( 'scheduled for: %s', 'insert-php' ),
272 '<strong>' . date_i18n( __( 'M j, Y @ G:i', 'insert-php' ), strtotime( $post->post_date ) ) . '</strong>'
273 ),
274 10 => $singular . ' ' . __( 'draft updated.', 'insert-php' ),
275 ];
276
277 return $messages;
278 }
279
280 /**
281 * Add capabilities to administrator role.
282 * Called on plugin activation.
283 *
284 * @return void
285 */
286 public function add_capabilities() {
287 if ( empty( $this->capabilities ) ) {
288 return;
289 }
290
291 foreach ( $this->capabilities as $role_name ) {
292 $role = get_role( $role_name );
293 if ( ! $role ) {
294 continue;
295 }
296
297 $role->add_cap( 'edit_' . $this->name );
298 $role->add_cap( 'read_' . $this->name );
299 $role->add_cap( 'delete_' . $this->name );
300 $role->add_cap( 'edit_' . $this->name . 's' );
301 $role->add_cap( 'edit_others_' . $this->name . 's' );
302 $role->add_cap( 'publish_' . $this->name . 's' );
303 $role->add_cap( 'read_private_' . $this->name . 's' );
304 }
305 }
306
307 /**
308 * Remove capabilities from all roles.
309 * Called on plugin deactivation.
310 *
311 * @return void
312 */
313 public function remove_capabilities() {
314 global $wp_roles;
315
316 if ( ! isset( $wp_roles ) ) {
317 return;
318 }
319
320 $all_roles = $wp_roles->roles;
321
322 foreach ( $all_roles as $role_name => $role_info ) {
323 $role = get_role( $role_name );
324 if ( ! $role ) {
325 continue;
326 }
327
328 $role->remove_cap( 'edit_' . $this->name );
329 $role->remove_cap( 'read_' . $this->name );
330 $role->remove_cap( 'delete_' . $this->name );
331 $role->remove_cap( 'edit_' . $this->name . 's' );
332 $role->remove_cap( 'edit_others_' . $this->name . 's' );
333 $role->remove_cap( 'publish_' . $this->name . 's' );
334 $role->remove_cap( 'read_private_' . $this->name . 's' );
335 }
336 }
337 }