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