PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.2.7
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.2.7
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
← All changes | admin/boot.php +206 -144 2.0.42.2.7 View file →
@@ -1,182 +1,244 @@
1 1 <?php
2 - /**
3 - * Admin boot
4 - * @author Alex Kovalev <alex.kovalevv@gmail.com>
5 - * @copyright Alex Kovalev 05.06.2018
6 - * @version 1.0
7 - */
2 +/**
3 + * Admin boot
4 + *
5 + * @author Alex Kovalev <alex.kovalevv@gmail.com>
6 + * @copyright Alex Kovalev 05.06.2018
7 + * @version 1.0
8 + */
8 9
9 - /**
10 - * Admin warning notices
11 - */
12 - function wbcr_inp_admin_conflict_notices_error($notices, $plugin_name)
13 - {
14 - if( $plugin_name != WINP_Plugin::app()->getPluginName() ) {
15 - return $notices;
16 - }
10 +// Exit if accessed directly
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit;
13 +}
17 14
18 - if( isset($_COOKIE['wbcr-php-snippets-safe-mode']) ) {
19 - $disable_safe_mode_url = add_query_arg(array('wbcr-php-snippets-disable-safe-mode' => 1));
15 +/**
16 + * Добавляет подсказку и кнопку в сообщение фатальной ошибки.
17 + *
18 + * С версии Wordpress 5.2, нам доступен специальный режим, перехвата php ошибок.
19 + * Если пользователь например допустит синтаксическую ошибку при редактировании
20 + * сниппета, то он вместо белого экрана (если php ошибки отключены на сервере)
21 + * увидит сообщение от Wordpress сгенерированное классом WP_Fatal_Error_Handler.
22 + *
23 + * Мы решили добавить в это сообщение кнопку для перехода в безопасный режим.
24 + */
25 +add_filter( 'wp_php_error_message', function ( $message ) {
26 + $safe_mode_url = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&wbcr-php-snippets-safe-mode' );
27 + $safe_mode_button = '<div style="margin:20px 0;padding:20px; background:#ffe8e8;">' . __( 'If you see this message after saving the snippet to the Woody ad snippets plugin, please enable safe mode in the Woody plugin. Safe mode will allow you to continue working in the admin panel of your site and change the snippet in which you made a php error.', 'insert_php' ) . '</div>';
28 + $safe_mode_button .= '<a href="' . $safe_mode_url . '" class="button">' . __( 'Enable safe mode in Woody ad snippets', 'insert_php' ) . '</a>';
20 29
21 - $safe_mode_notice = WINP_Plugin::app()
22 - ->getPluginTitle() . ': ' . __('Running in safe mode. This mode your snippets will not be started.', 'insert-php');
23 - $safe_mode_notice .= ' <a href="' . $disable_safe_mode_url . '" class="button button-default">Disable Safe Mode</a>';
30 + return $message . $safe_mode_button;
31 +} );
24 32
25 - $notices[] = array(
26 - 'id' => 'inp_safe_mode',
27 - 'type' => 'success',
28 - 'dismissible' => false,
29 - 'dismiss_expires' => 0,
30 - 'text' => '<p>' . $safe_mode_notice . '</p>'
31 - );
32 - }
33 +/**
34 + * Инициализации метабоксов и страницы "о плагине".
35 + *
36 + * Этот хук реализует условную логику, при которой пользователь переодически будет
37 + * видет страницу "О плагине", а конкретно при активации и обновлении плагина.
38 + */
39 +add_action( 'admin_init', function () {
33 40
34 - $create_notice_url = admin_url('edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE);
41 + $plugin = WINP_Plugin::app();
35 42
36 - $upgrade_plugin_notice = '<b>' . WINP_Plugin::app()
37 - ->getPluginTitle() . '</b>: ' . __('Attention! This new 2.0 plugin version, we added the ability to insert php code using snippets. This is a more convenient and secure way than using shortcodes [insert_php] code execute [/ insert_php]. However, for compatibility reasons, we left support for [insert_php] shortcodes, but we will depreciate them in the next versions of the plugin.', 'insert-php');
43 + // Register metaboxes
44 + require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/base-options.php' );
45 + WINP_Helper::register_factory_metaboxes( new WINP_BaseOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin );
38 46
39 - $upgrade_plugin_notice .= '<br><br>' . __('We strongly recommend you to transfer your php code to snippets and call them in your posts/pages and widgets using [wbcr_php_snippet id = "000"] shortcodes.', 'insert-php');
47 + //if ( current_user_can( 'install_plugins' ) && ! is_plugin_active( 'robin-image-optimizer/robin-image-optimizer.php' ) ) {
48 + if ( ! WINP_Plugin::app()->premium->is_activate() ) {
49 + require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/info.php' );
50 + WINP_Helper::register_factory_metaboxes( new WINP_InfoMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin );
51 + }
40 52
41 - //$upgrade_plugin_notice .= '<br><br><span style="color:red">' . __('If you have updated from version 1.3 of the plugin (Insert php). Please deactivate and activate the plugin to update the settings.', 'insert-php') . '</span>';
53 + $snippet_type = WINP_Helper::get_snippet_type();
42 54
43 - $upgrade_plugin_notice .= '<br><br><a href="' . $create_notice_url . '" class="button button-default">' . __('Create new php snippet', 'insert-php') . '</a> ';
55 + if ( $snippet_type !== WINP_SNIPPET_TYPE_PHP ) {
56 + require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/view-options.php' );
57 + WINP_Helper::register_factory_metaboxes( new WINP_ViewOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin );
58 + }
44 59
45 - $upgrade_plugin_notice .= '<a href="https://downloads.wordpress.org/plugin/insert-php.1.3.zip" class="button button-default">' . __('Download old version', 'insert-php') . '</a><br><br>';
46 - $upgrade_plugin_notice .= sprintf(__('If you have issues with the plugin new version or any suggestions, please contact us on <a href="%s" target="_blank">our forum</a>.', 'insert-php'), 'https://wordpress.org/support/plugin/insert-php');
47 - $upgrade_plugin_notice .= '<br>' . sprintf(__('We tried to make the plugin better and more convenient. If you like the new version, feel free to <a href="%s" target="_blank">leave your feedback</a>. It will give us motivation for further improvements.', 'insert-php'), 'https://wordpress.org/support/plugin/insert-php/reviews/#new-post');
60 + do_action( 'wbcr/inp/boot/metaboxes/revisions', '' );
48 61
49 - $notices[] = array(
50 - 'id' => 'inp_upgrade_plugin',
51 - 'type' => 'warning',
52 - 'dismissible' => true,
53 - 'dismiss_expires' => 0,
54 - 'text' => '<p>' . $upgrade_plugin_notice . '</p>'
55 - );
56 -
57 - return $notices;
62 + // If the user has updated the plugin or activated it for the first time,
63 + // you need to show the page "What's new?"
64 + if ( ! WINP_Plugin::app()->isNetworkAdmin() ) {
65 + $about_page_viewed = WINP_Plugin::app()->request->get( 'wbcr_inp_about_page_viewed', null );
66 + if ( is_null( $about_page_viewed ) ) {
67 + if ( WINP_Helper::is_need_show_about_page() ) {
68 + try {
69 + $redirect_url = '';
70 + if ( class_exists( 'Wbcr_FactoryPages419' ) ) {
71 + $redirect_url = WINP_Plugin::app()->getPluginPageUrl( 'about', [ 'wbcr_inp_about_page_viewed' => 1 ] );
72 + }
73 + if ( $redirect_url ) {
74 + wp_safe_redirect( $redirect_url );
75 + die();
76 + }
77 + } catch( Exception $e ) {
78 + }
79 + }
80 + } else {
81 + if ( WINP_Helper::is_need_show_about_page() ) {
82 + delete_option( $plugin->getOptionName( 'what_new_210' ) );
83 + }
84 + }
58 85 }
86 +} );
59 87
60 - add_filter('wbcr_factory_admin_notices', 'wbcr_inp_admin_conflict_notices_error', 10, 2);
88 +function wbcr_inp_admin_revisions() {
89 + $plugin = WINP_Plugin::app();
61 90
62 - function wbcr_inp_admin_init()
63 - {
64 - $plugin = WINP_Plugin::app();
91 + require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/revisions.php' );
92 + WINP_Helper::register_factory_metaboxes( new WINP_RevisionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin );
93 +}
65 94
66 - // Disable safe mode
95 +add_action( 'wbcr/inp/boot/metaboxes/revisions', 'wbcr_inp_admin_revisions' );
67 96
68 - if( isset($_COOKIE['wbcr-php-snippets-safe-mode']) && isset($_REQUEST['wbcr-php-snippets-disable-safe-mode']) && $plugin->currentUserCan() ) {
69 - unset($_COOKIE['wbcr-php-snippets-safe-mode']);
70 - setcookie('wbcr-php-snippets-safe-mode', null, -1);
71 - wp_safe_redirect(remove_query_arg(array('wbcr-php-snippets-disable-safe-mode')));
72 - exit;
73 - }
97 +// ---
98 +// Editor
99 +//
74 100
75 - // Register metaboxes
101 +/**
102 + * Enqueue scripts
103 + */
104 +function wbcr_inp_enqueue_scripts() {
105 + global $pagenow;
76 106
77 - require_once(WINP_PLUGIN_DIR . '/admin/metaboxes/base-options.php');
78 - Wbcr_FactoryMetaboxes400::registerFor(new WINP_BaseOptionsMetaBox($plugin), WINP_SNIPPETS_POST_TYPE, $plugin);
107 + $screen = get_current_screen();
79 108
80 - // Upgrade up to new version
81 - if( !$plugin->getOption('upgrade_up_to_201', false) ) {
82 - $role = get_role('administrator');
83 - if( !$role ) {
84 - return;
85 - }
109 + if ( ( 'post-new.php' == $pagenow || 'post.php' == $pagenow ) && WINP_SNIPPETS_POST_TYPE == $screen->post_type ) {
110 + wp_enqueue_script( 'wbcr-inp-admin-scripts', WINP_PLUGIN_URL . '/admin/assets/js/scripts.js', [
111 + 'jquery',
112 + 'jquery-ui-tooltip'
113 + ], WINP_Plugin::app()->getPluginVersion() );
114 + }
115 +}
86 116
87 - $role->add_cap('edit_' . WINP_SNIPPETS_POST_TYPE);
88 - $role->add_cap('read_' . WINP_SNIPPETS_POST_TYPE);
89 - $role->add_cap('delete_' . WINP_SNIPPETS_POST_TYPE);
90 - $role->add_cap('edit_' . WINP_SNIPPETS_POST_TYPE . 's');
91 - $role->add_cap('edit_others_' . WINP_SNIPPETS_POST_TYPE . 's');
92 - $role->add_cap('publish_' . WINP_SNIPPETS_POST_TYPE . 's');
93 - $role->add_cap('read_private_' . WINP_SNIPPETS_POST_TYPE . 's');
117 +/**
118 + * Asset scripts for the tinymce editor
119 + *
120 + * @param string $hook
121 + */
122 +function wbcr_inp_enqueue_tinymce_assets( $hook ) {
123 + $pages = [
124 + 'post.php',
125 + 'post-new.php',
126 + 'widgets.php'
127 + ];
94 128
95 - $plugin->updateOption('upgrade_up_to_201', 1);
96 - }
129 + if ( ! in_array( $hook, $pages ) || ! current_user_can( 'manage_options' ) ) {
130 + return;
97 131 }
98 132
99 - add_action('admin_init', 'wbcr_inp_admin_init');
133 + wp_enqueue_script( 'wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', [ 'jquery' ], WINP_Plugin::app()->getPluginVersion(), true );
134 +}
100 135
101 - // ---
102 - // Editor
103 - //
136 +add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_tinymce_assets' );
137 +add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_scripts' );
104 138
105 - /**
106 - * Asset scripts for the tinymce editor
107 - *
108 - * @param string $hook
109 - */
110 - function wbcr_inp_enqueue_tinymce_assets($hook)
111 - {
112 - $pages = array(
113 - 'post.php',
114 - 'post-new.php',
115 - 'widgets.php'
116 - );
139 +/**
140 + * Adds js variable required for shortcodes.
141 + *
142 + * @since 1.1.0
143 + * @see before_wp_tiny_mce
144 + */
145 +function wbcr_inp_tinymce_data( $hook ) {
146 + if ( ! current_user_can( 'manage_options' ) ) {
147 + return;
148 + }
117 149
118 - if( !in_array($hook, $pages) || !current_user_can('manage_options') ) {
119 - return;
120 - }
150 + // styles for the plugin shorcodes
151 + $shortcode_icon = WINP_PLUGIN_URL . '/admin/assets/img/shortcode-icon5.png';
152 + $shortcode_title = __( 'Woody ad snippets', 'insert-php' );
121 153
122 - wp_enqueue_script('wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', array('jquery'), WINP_Plugin::app()
123 - ->getPluginVersion(), true);
154 + $result = WINP_Helper::get_shortcode_data( true );
155 + $shortcode_snippets_json = json_encode( $result );
156 + ?>
157 + <!-- <?php echo WINP_Plugin::app()->getPluginTitle() ?> for tinymce -->
158 + <style>
159 + i.wbcr-inp-shortcode-icon {
160 + background: url("<?php echo $shortcode_icon ?>") center no-repeat;
161 + }
162 + </style>
163 + <script>
164 + var wbcr_inp_tinymce_snippets_button_title = '<?php echo $shortcode_title ?>';
165 + var wbcr_inp_post_tinymce_nonce = '<?php echo wp_create_nonce( 'wbcr_inp_tinymce_post_nonce' ) ?>';
166 + var wbcr_inp_shortcode_snippets = <?php echo $shortcode_snippets_json ?>;
167 + </script>
168 + <!-- /end <?php echo WINP_Plugin::app()->getPluginTitle() ?> for tinymce -->
169 + <?php
170 +}
171 +
172 +add_action( 'admin_print_scripts-post.php', 'wbcr_inp_tinymce_data' );
173 +add_action( 'admin_print_scripts-post-new.php', 'wbcr_inp_tinymce_data' );
174 +add_action( 'admin_print_scripts-widgets.php', 'wbcr_inp_tinymce_data' );
175 +
176 +/**
177 + * Deactivate snippet on trashed
178 + *
179 + * @since 2.0.6
180 + *
181 + * @param $post_id
182 + *
183 + */
184 +function wbcr_inp_trash_post( $post_id ) {
185 + $post_type = get_post_type( $post_id );
186 + if ( $post_type == WINP_SNIPPETS_POST_TYPE ) {
187 + WINP_Helper::updateMetaOption( $post_id, 'snippet_activate', 0 );
124 188 }
189 +}
125 190
126 - add_action('admin_enqueue_scripts', 'wbcr_inp_enqueue_tinymce_assets');
191 +add_action( 'wp_trash_post', 'wbcr_inp_trash_post' );
127 192
128 - /**
129 - * Adds js variable required for shortcodes.
130 - *
131 - * @see before_wp_tiny_mce
132 - * @since 1.1.0
133 - */
134 - function wbcr_inp_tinymce_data($hook)
135 - {
136 - if( !current_user_can('manage_options') ) {
137 - return;
138 - }
193 +/**
194 + * Removes the default 'new item' from the admin menu to add own page 'new item' later.
195 + *
196 + * @param $menu
197 + *
198 + * @return mixed
199 + * @see menu_order
200 + *
201 + */
202 +function wbcr_inp_remove_new_item( $menu ) {
203 + global $submenu;
139 204
140 - // styles for the plugin shorcodes
141 - $shortcode_icon = WINP_PLUGIN_URL . '/admin/assets/img/shortcode-icon.png';
142 - $shortcode_title = __('PHP snippets', 'insert-php');
205 + if ( ! isset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ] ) ) {
206 + return $menu;
207 + }
208 + unset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ][10] );
143 209
144 - $snippets = get_posts(array(
145 - 'post_type' => WINP_SNIPPETS_POST_TYPE,
146 - 'meta_key' => WINP_Plugin::app()->getPrefix() . 'snippet_scope',
147 - 'meta_value' => 'shortcode',
148 - 'post_status' => 'publish',
149 - 'numberposts' => -1
150 - ));
210 + return $menu;
211 +}
151 212
152 - $result = array();
153 - foreach((array)$snippets as $snippet) {
154 - $result[] = array(
155 - 'id' => $snippet->ID,
156 - 'title' => empty($snippet->post_title)
157 - ? '(no titled, ID=' . $snippet->ID . ')'
158 - : $snippet->post_title
159 - );
160 - }
213 +add_filter( 'custom_menu_order', '__return_true' );
214 +add_filter( 'admin_menu', 'wbcr_inp_remove_new_item' );
161 215
162 - $shortcode_snippets_json = json_encode($result);
216 +/**
217 + * If the user tried to get access to the default 'new item',
218 + * redirects forcibly to our page 'new item'.
219 + *
220 + * @see current_screen
221 + */
222 +function wbcr_inp_redirect_to_new_item() {
223 + $screen = get_current_screen();
163 224
164 - ?>
165 - <!-- <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce -->
166 - <style>
167 - i.wbcr-inp-shortcode-icon {
168 - background: url("<?php echo $shortcode_icon ?>") center no-repeat;
169 - }
170 - </style>
171 - <script>
172 - var wbcr_inp_tinymce_snippets_button_title = '<?php echo $shortcode_title ?>';
173 - var wbcr_inp_post_tinymce_nonce = '<?php echo wp_create_nonce('wbcr_inp_tinymce_post_nonce') ?>';
174 - var wbcr_inp_shortcode_snippets = <?= $shortcode_snippets_json ?>;
175 - </script>
176 - <!-- /end <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce -->
177 - <?php
225 + if ( empty( $screen ) ) {
226 + return;
178 227 }
228 + if ( 'add' !== $screen->action || 'post' !== $screen->base || WINP_SNIPPETS_POST_TYPE !== $screen->post_type ) {
229 + return;
230 + }
179 231
180 - add_action('admin_print_scripts-post.php', 'wbcr_inp_tinymce_data');
181 - add_action('admin_print_scripts-post-new.php', 'wbcr_inp_tinymce_data');
182 - add_action('admin_print_scripts-widgets.php', 'wbcr_inp_tinymce_data');
232 + $winp_item = WINP_Plugin::app()->request->get( 'winp_item', null );
233 + if ( ! is_null( $winp_item ) ) {
234 + return;
235 + }
236 +
237 + $url = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=new-item-' . WINP_Plugin::app()->getPluginName() );
238 +
239 + wp_safe_redirect( $url );
240 +
241 + exit;
242 +}
243 +
244 +add_action( 'current_screen', 'wbcr_inp_redirect_to_new_item' );