| @@ -1,102 +1,99 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Admin boot |
| 4 | - * @author Alex Kovalev <alex.kovalevv@gmail.com> | |
| 4 | + * | |
| 5 | + * @author Alex Kovalev <alex.kovalevv@gmail.com> | |
| 5 | 6 | * @copyright Alex Kovalev 05.06.2018 |
| 6 | - * @version 1.0 | |
| 7 | + * @version 1.0 | |
| 7 | 8 | */ |
| 8 | 9 | |
| 10 | +// Exit if accessed directly | |
| 11 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit; | |
| 13 | +} | |
| 14 | + | |
| 9 | 15 | /** |
| 10 | - * Enables/Disable safe mode, in which the php code will not be executed. | |
| 16 | + * Добавляет подсказку и кнопку в сообщение фатальной ошибки. | |
| 17 | + * | |
| 18 | + * С версии Wordpress 5.2, нам доступен специальный режим, перехвата php ошибок. | |
| 19 | + * Если пользователь например допустит синтаксическую ошибку при редактировании | |
| 20 | + * сниппета, то он вместо белого экрана (если php ошибки отключены на сервере) | |
| 21 | + * увидит сообщение от Wordpress сгенерированное классом WP_Fatal_Error_Handler. | |
| 22 | + * | |
| 23 | + * Мы решили добавить в это сообщение кнопку для перехода в безопасный режим. | |
| 11 | 24 | */ |
| 12 | -function wbcr_inp_safe_mode() { | |
| 13 | - if ( isset( $_GET['wbcr-php-snippets-safe-mode'] ) ) { | |
| 14 | - WINP_Helper::enable_safe_mode(); | |
| 15 | - } | |
| 16 | - if ( isset( $_GET['wbcr-php-snippets-disable-safe-mode'] ) ) { | |
| 17 | - WINP_Helper::disable_safe_mode(); | |
| 18 | - } | |
| 19 | -} | |
| 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 | -add_action( 'plugins_loaded', 'wbcr_inp_safe_mode', - 1 ); | |
| 30 | + return $message . $safe_mode_button; | |
| 31 | +} ); | |
| 22 | 32 | |
| 23 | -function wbcr_inp_admin_init() { | |
| 24 | - | |
| 33 | +/** | |
| 34 | + * Инициализации метабоксов и страницы "о плагине". | |
| 35 | + * | |
| 36 | + * Этот хук реализует условную логику, при которой пользователь переодически будет | |
| 37 | + * видет страницу "О плагине", а конкретно при активации и обновлении плагина. | |
| 38 | + */ | |
| 39 | +add_action( 'admin_init', function () { | |
| 40 | + | |
| 25 | 41 | $plugin = WINP_Plugin::app(); |
| 26 | - | |
| 42 | + | |
| 27 | 43 | // Register metaboxes |
| 28 | 44 | require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/base-options.php' ); |
| 29 | - Wbcr_FactoryMetaboxes404::registerFor( new WINP_BaseOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 30 | - | |
| 31 | - if ( current_user_can( 'install_plugins' ) && ! is_plugin_active( 'robin-image-optimizer/robin-image-optimizer.php' ) ) { | |
| 45 | + WINP_Helper::register_factory_metaboxes( new WINP_BaseOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 46 | + | |
| 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() ) { | |
| 32 | 49 | require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/info.php' ); |
| 33 | - Wbcr_FactoryMetaboxes404::registerFor( new WINP_InfoMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 50 | + WINP_Helper::register_factory_metaboxes( new WINP_InfoMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 34 | 51 | } |
| 35 | - | |
| 52 | + | |
| 36 | 53 | $snippet_type = WINP_Helper::get_snippet_type(); |
| 37 | - | |
| 54 | + | |
| 38 | 55 | if ( $snippet_type !== WINP_SNIPPET_TYPE_PHP ) { |
| 39 | 56 | require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/view-options.php' ); |
| 40 | - Wbcr_FactoryMetaboxes404::registerFor( new WINP_ViewOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 57 | + WINP_Helper::register_factory_metaboxes( new WINP_ViewOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 41 | 58 | } |
| 42 | - | |
| 43 | - // Upgrade up to new version | |
| 44 | - $first_activation = get_option( $plugin->getOptionName( 'first_activation' ) ); | |
| 45 | - $is_upgraded = get_option( $plugin->getOptionName( 'upgrade_up_to_201' ) ); | |
| 46 | - | |
| 47 | - if ( ! $first_activation && ! $is_upgraded ) { | |
| 48 | - $role = get_role( 'administrator' ); | |
| 49 | - | |
| 50 | - if ( ! $role ) { | |
| 51 | - return; | |
| 52 | - } | |
| 53 | - | |
| 54 | - $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE ); | |
| 55 | - $role->add_cap( 'read_' . WINP_SNIPPETS_POST_TYPE ); | |
| 56 | - $role->add_cap( 'delete_' . WINP_SNIPPETS_POST_TYPE ); | |
| 57 | - $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE . 's' ); | |
| 58 | - $role->add_cap( 'edit_others_' . WINP_SNIPPETS_POST_TYPE . 's' ); | |
| 59 | - $role->add_cap( 'publish_' . WINP_SNIPPETS_POST_TYPE . 's' ); | |
| 60 | - $role->add_cap( 'read_private_' . WINP_SNIPPETS_POST_TYPE . 's' ); | |
| 61 | - | |
| 62 | - update_option( $plugin->getOptionName( 'upgrade_up_to_201' ), 1 ); | |
| 63 | - update_option( $plugin->getOptionName( 'what_new_210' ), 1 ); | |
| 64 | - | |
| 65 | - // Create a demo snippets with examples of use | |
| 66 | - if ( ! get_option( $plugin->getOptionName( 'demo_snippets_created' ) ) ) { | |
| 67 | - WINP_Helper::create_demo_snippets(); | |
| 68 | - } | |
| 69 | - | |
| 70 | - // Write information about the first activation of plugin | |
| 71 | - if ( ! $first_activation ) { | |
| 72 | - update_option( $plugin->getOptionName( 'first_activation' ), array( | |
| 73 | - 'version' => $plugin->getPluginVersion(), | |
| 74 | - 'timestamp' => time() | |
| 75 | - ) ); | |
| 76 | - } | |
| 77 | - } | |
| 78 | - | |
| 59 | + | |
| 60 | + do_action( 'wbcr/inp/boot/metaboxes/revisions', '' ); | |
| 61 | + | |
| 79 | 62 | // If the user has updated the plugin or activated it for the first time, |
| 80 | 63 | // you need to show the page "What's new?" |
| 81 | - | |
| 82 | - if ( ! isset( $_GET['wbcr_inp_about_page_viewed'] ) ) { | |
| 83 | - if ( WINP_Helper::is_need_show_about_page() ) { | |
| 84 | - try { | |
| 85 | - $redirect_url = WINP_Plugin::app()->getPluginPageUrl( 'about', array( 'wbcr_inp_about_page_viewed' => 1 ) ); | |
| 86 | - wp_safe_redirect( $redirect_url ); | |
| 87 | - die(); | |
| 88 | - } catch( Exception $e ) { | |
| 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 | + } | |
| 89 | 79 | } |
| 80 | + } else { | |
| 81 | + if ( WINP_Helper::is_need_show_about_page() ) { | |
| 82 | + delete_option( $plugin->getOptionName( 'what_new_210' ) ); | |
| 83 | + } | |
| 90 | 84 | } |
| 91 | - } else { | |
| 92 | - if ( WINP_Helper::is_need_show_about_page() ) { | |
| 93 | - delete_option( $plugin->getOptionName( 'what_new_210' ) ); | |
| 94 | - } | |
| 95 | 85 | } |
| 86 | +} ); | |
| 87 | + | |
| 88 | +function wbcr_inp_admin_revisions() { | |
| 89 | + $plugin = WINP_Plugin::app(); | |
| 90 | + | |
| 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 ); | |
| 96 | 93 | } |
| 97 | 94 | |
| 98 | -add_action( 'admin_init', 'wbcr_inp_admin_init' ); | |
| 95 | +add_action( 'wbcr/inp/boot/metaboxes/revisions', 'wbcr_inp_admin_revisions' ); | |
| 99 | 96 | |
| 100 | 97 | // --- |
| 101 | 98 | // Editor |
| 102 | 99 | // |
| @@ -105,16 +102,16 @@ | ||
| 105 | 102 | * Enqueue scripts |
| 106 | 103 | */ |
| 107 | 104 | function wbcr_inp_enqueue_scripts() { |
| 108 | 105 | global $pagenow; |
| 109 | - | |
| 106 | + | |
| 110 | 107 | $screen = get_current_screen(); |
| 111 | - | |
| 108 | + | |
| 112 | 109 | if ( ( 'post-new.php' == $pagenow || 'post.php' == $pagenow ) && WINP_SNIPPETS_POST_TYPE == $screen->post_type ) { |
| 113 | - wp_enqueue_script( 'wbcr-inp-admin-scripts', WINP_PLUGIN_URL . '/admin/assets/js/scripts.js', array( | |
| 110 | + wp_enqueue_script( 'wbcr-inp-admin-scripts', WINP_PLUGIN_URL . '/admin/assets/js/scripts.js', [ | |
| 114 | 111 | 'jquery', |
| 115 | 112 | 'jquery-ui-tooltip' |
| 116 | - ), WINP_Plugin::app()->getPluginVersion() ); | |
| 113 | + ], WINP_Plugin::app()->getPluginVersion() ); | |
| 117 | 114 | } |
| 118 | 115 | } |
| 119 | 116 | |
| 120 | 117 | /** |
| @@ -122,19 +119,19 @@ | ||
| 122 | 119 | * |
| 123 | 120 | * @param string $hook |
| 124 | 121 | */ |
| 125 | 122 | function wbcr_inp_enqueue_tinymce_assets( $hook ) { |
| 126 | - $pages = array( | |
| 123 | + $pages = [ | |
| 127 | 124 | 'post.php', |
| 128 | 125 | 'post-new.php', |
| 129 | 126 | 'widgets.php' |
| 130 | - ); | |
| 131 | - | |
| 127 | + ]; | |
| 128 | + | |
| 132 | 129 | if ( ! in_array( $hook, $pages ) || ! current_user_can( 'manage_options' ) ) { |
| 133 | 130 | return; |
| 134 | 131 | } |
| 135 | - | |
| 136 | - wp_enqueue_script( 'wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', array( 'jquery' ), WINP_Plugin::app()->getPluginVersion(), true ); | |
| 132 | + | |
| 133 | + wp_enqueue_script( 'wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', [ 'jquery' ], WINP_Plugin::app()->getPluginVersion(), true ); | |
| 137 | 134 | } |
| 138 | 135 | |
| 139 | 136 | add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_tinymce_assets' ); |
| 140 | 137 | add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_scripts' ); |
| @@ -141,25 +138,24 @@ | ||
| 141 | 138 | |
| 142 | 139 | /** |
| 143 | 140 | * Adds js variable required for shortcodes. |
| 144 | 141 | * |
| 145 | - * @see before_wp_tiny_mce | |
| 146 | 142 | * @since 1.1.0 |
| 143 | + * @see before_wp_tiny_mce | |
| 147 | 144 | */ |
| 148 | 145 | function wbcr_inp_tinymce_data( $hook ) { |
| 149 | 146 | if ( ! current_user_can( 'manage_options' ) ) { |
| 150 | 147 | return; |
| 151 | 148 | } |
| 152 | - | |
| 149 | + | |
| 153 | 150 | // styles for the plugin shorcodes |
| 154 | 151 | $shortcode_icon = WINP_PLUGIN_URL . '/admin/assets/img/shortcode-icon5.png'; |
| 155 | 152 | $shortcode_title = __( 'Woody ad snippets', 'insert-php' ); |
| 156 | - | |
| 157 | - $result = WINP_Helper::get_shortcode_data(); | |
| 153 | + | |
| 154 | + $result = WINP_Helper::get_shortcode_data( true ); | |
| 158 | 155 | $shortcode_snippets_json = json_encode( $result ); |
| 159 | - | |
| 160 | 156 | ?> |
| 161 | - <!-- <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 157 | + <!-- <?php echo WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 162 | 158 | <style> |
| 163 | 159 | i.wbcr-inp-shortcode-icon { |
| 164 | 160 | background: url("<?php echo $shortcode_icon ?>") center no-repeat; |
| 165 | 161 | } |
| @@ -166,11 +162,11 @@ | ||
| 166 | 162 | </style> |
| 167 | 163 | <script> |
| 168 | 164 | var wbcr_inp_tinymce_snippets_button_title = '<?php echo $shortcode_title ?>'; |
| 169 | 165 | var wbcr_inp_post_tinymce_nonce = '<?php echo wp_create_nonce( 'wbcr_inp_tinymce_post_nonce' ) ?>'; |
| 170 | - var wbcr_inp_shortcode_snippets = <?= $shortcode_snippets_json ?>; | |
| 166 | + var wbcr_inp_shortcode_snippets = <?php echo $shortcode_snippets_json ?>; | |
| 171 | 167 | </script> |
| 172 | - <!-- /end <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 168 | + <!-- /end <?php echo WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 173 | 169 | <?php |
| 174 | 170 | } |
| 175 | 171 | |
| 176 | 172 | add_action( 'admin_print_scripts-post.php', 'wbcr_inp_tinymce_data' ); |
| @@ -179,11 +175,12 @@ | ||
| 179 | 175 | |
| 180 | 176 | /** |
| 181 | 177 | * Deactivate snippet on trashed |
| 182 | 178 | * |
| 179 | + * @since 2.0.6 | |
| 180 | + * | |
| 183 | 181 | * @param $post_id |
| 184 | 182 | * |
| 185 | - * @since 2.0.6 | |
| 186 | 183 | */ |
| 187 | 184 | function wbcr_inp_trash_post( $post_id ) { |
| 188 | 185 | $post_type = get_post_type( $post_id ); |
| 189 | 186 | if ( $post_type == WINP_SNIPPETS_POST_TYPE ) { |
| @@ -195,27 +192,27 @@ | ||
| 195 | 192 | |
| 196 | 193 | /** |
| 197 | 194 | * Removes the default 'new item' from the admin menu to add own page 'new item' later. |
| 198 | 195 | * |
| 199 | - * @see menu_order | |
| 200 | - * | |
| 201 | 196 | * @param $menu |
| 202 | 197 | * |
| 203 | 198 | * @return mixed |
| 199 | + * @see menu_order | |
| 200 | + * | |
| 204 | 201 | */ |
| 205 | 202 | function wbcr_inp_remove_new_item( $menu ) { |
| 206 | 203 | global $submenu; |
| 207 | - | |
| 204 | + | |
| 208 | 205 | if ( ! isset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ] ) ) { |
| 209 | 206 | return $menu; |
| 210 | 207 | } |
| 211 | 208 | unset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ][10] ); |
| 212 | - | |
| 209 | + | |
| 213 | 210 | return $menu; |
| 214 | 211 | } |
| 215 | 212 | |
| 216 | 213 | add_filter( 'custom_menu_order', '__return_true' ); |
| 217 | -add_filter( 'menu_order', 'wbcr_inp_remove_new_item' ); | |
| 214 | +add_filter( 'admin_menu', 'wbcr_inp_remove_new_item' ); | |
| 218 | 215 | |
| 219 | 216 | /** |
| 220 | 217 | * If the user tried to get access to the default 'new item', |
| 221 | 218 | * redirects forcibly to our page 'new item'. |
| @@ -223,9 +220,9 @@ | ||
| 223 | 220 | * @see current_screen |
| 224 | 221 | */ |
| 225 | 222 | function wbcr_inp_redirect_to_new_item() { |
| 226 | 223 | $screen = get_current_screen(); |
| 227 | - | |
| 224 | + | |
| 228 | 225 | if ( empty( $screen ) ) { |
| 229 | 226 | return; |
| 230 | 227 | } |
| 231 | 228 | if ( 'add' !== $screen->action || 'post' !== $screen->base || WINP_SNIPPETS_POST_TYPE !== $screen->post_type ) { |
| @@ -230,16 +227,18 @@ | ||
| 230 | 227 | } |
| 231 | 228 | if ( 'add' !== $screen->action || 'post' !== $screen->base || WINP_SNIPPETS_POST_TYPE !== $screen->post_type ) { |
| 232 | 229 | return; |
| 233 | 230 | } |
| 234 | - if ( isset( $_GET['winp_item'] ) ) { | |
| 231 | + | |
| 232 | + $winp_item = WINP_Plugin::app()->request->get( 'winp_item', null ); | |
| 233 | + if ( ! is_null( $winp_item ) ) { | |
| 235 | 234 | return; |
| 236 | 235 | } |
| 237 | - | |
| 236 | + | |
| 238 | 237 | $url = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=new-item-' . WINP_Plugin::app()->getPluginName() ); |
| 239 | - | |
| 238 | + | |
| 240 | 239 | wp_safe_redirect( $url ); |
| 241 | - | |
| 240 | + | |
| 242 | 241 | exit; |
| 243 | 242 | } |
| 244 | 243 | |
| 245 | -add_action( 'current_screen', 'wbcr_inp_redirect_to_new_item' ); | |
| 244 | +add_action( 'current_screen', 'wbcr_inp_redirect_to_new_item' ); | |