| @@ -1,222 +1,215 @@ | ||
| 1 | -<?php | |
| 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 | - */ | |
| 9 | - | |
| 10 | -// Exit if accessed directly | |
| 11 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | - exit; | |
| 13 | -} | |
| 14 | - | |
| 15 | -function wbcr_inp_admin_init() { | |
| 16 | - | |
| 17 | - $plugin = WINP_Plugin::app(); | |
| 18 | - | |
| 19 | - // Register metaboxes | |
| 20 | - require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/base-options.php' ); | |
| 21 | - WINP_Helper::register_factory_metaboxes( new WINP_BaseOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 22 | - | |
| 23 | - //if ( current_user_can( 'install_plugins' ) && ! is_plugin_active( 'robin-image-optimizer/robin-image-optimizer.php' ) ) { | |
| 24 | - if ( ! WINP_Plugin::app()->premium->is_activate() ) { | |
| 25 | - require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/info.php' ); | |
| 26 | - WINP_Helper::register_factory_metaboxes( new WINP_InfoMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 27 | - } | |
| 28 | - | |
| 29 | - $snippet_type = WINP_Helper::get_snippet_type(); | |
| 30 | - | |
| 31 | - if ( $snippet_type !== WINP_SNIPPET_TYPE_PHP ) { | |
| 32 | - require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/view-options.php' ); | |
| 33 | - WINP_Helper::register_factory_metaboxes( new WINP_ViewOptionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 34 | - } | |
| 35 | - | |
| 36 | - do_action( 'wbcr/inp/boot/metaboxes/revisions', '' ); | |
| 37 | - | |
| 38 | - // If the user has updated the plugin or activated it for the first time, | |
| 39 | - // you need to show the page "What's new?" | |
| 40 | - if ( ! WINP_Plugin::app()->isNetworkAdmin() ) { | |
| 41 | - $about_page_viewed = WINP_Plugin::app()->request->get( 'wbcr_inp_about_page_viewed', null ); | |
| 42 | - if ( is_null( $about_page_viewed ) ) { | |
| 43 | - if ( WINP_Helper::is_need_show_about_page() ) { | |
| 44 | - try { | |
| 45 | - $redirect_url = ''; | |
| 46 | - if ( class_exists( 'Wbcr_FactoryPages413' ) ) { | |
| 47 | - $redirect_url = WINP_Plugin::app()->getPluginPageUrl( 'about', [ 'wbcr_inp_about_page_viewed' => 1 ] ); | |
| 48 | - } | |
| 49 | - if ( $redirect_url ) { | |
| 50 | - wp_safe_redirect( $redirect_url ); | |
| 51 | - die(); | |
| 52 | - } | |
| 53 | - } catch( Exception $e ) { | |
| 54 | - } | |
| 55 | - } | |
| 56 | - } else { | |
| 57 | - if ( WINP_Helper::is_need_show_about_page() ) { | |
| 58 | - delete_option( $plugin->getOptionName( 'what_new_210' ) ); | |
| 59 | - } | |
| 60 | - } | |
| 61 | - } | |
| 62 | -} | |
| 63 | - | |
| 64 | -add_action( 'admin_init', 'wbcr_inp_admin_init' ); | |
| 65 | - | |
| 66 | -function wbcr_inp_admin_revisions() { | |
| 67 | - $plugin = WINP_Plugin::app(); | |
| 68 | - | |
| 69 | - require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/revisions.php' ); | |
| 70 | - WINP_Helper::register_factory_metaboxes( new WINP_RevisionsMetaBox( $plugin ), WINP_SNIPPETS_POST_TYPE, $plugin ); | |
| 71 | -} | |
| 72 | - | |
| 73 | -add_action( 'wbcr/inp/boot/metaboxes/revisions', 'wbcr_inp_admin_revisions' ); | |
| 74 | - | |
| 75 | -// --- | |
| 76 | -// Editor | |
| 77 | -// | |
| 78 | - | |
| 79 | -/** | |
| 80 | - * Enqueue scripts | |
| 81 | - */ | |
| 82 | -function wbcr_inp_enqueue_scripts() { | |
| 83 | - global $pagenow; | |
| 84 | - | |
| 85 | - $screen = get_current_screen(); | |
| 86 | - | |
| 87 | - if ( ( 'post-new.php' == $pagenow || 'post.php' == $pagenow ) && WINP_SNIPPETS_POST_TYPE == $screen->post_type ) { | |
| 88 | - wp_enqueue_script( 'wbcr-inp-admin-scripts', WINP_PLUGIN_URL . '/admin/assets/js/scripts.js', [ | |
| 89 | - 'jquery', | |
| 90 | - 'jquery-ui-tooltip' | |
| 91 | - ], WINP_Plugin::app()->getPluginVersion() ); | |
| 92 | - } | |
| 93 | -} | |
| 94 | - | |
| 95 | -/** | |
| 96 | - * Asset scripts for the tinymce editor | |
| 97 | - * | |
| 98 | - * @param string $hook | |
| 99 | - */ | |
| 100 | -function wbcr_inp_enqueue_tinymce_assets( $hook ) { | |
| 101 | - $pages = [ | |
| 102 | - 'post.php', | |
| 103 | - 'post-new.php', | |
| 104 | - 'widgets.php' | |
| 105 | - ]; | |
| 106 | - | |
| 107 | - if ( ! in_array( $hook, $pages ) || ! current_user_can( 'manage_options' ) ) { | |
| 108 | - return; | |
| 109 | - } | |
| 110 | - | |
| 111 | - wp_enqueue_script( 'wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', [ 'jquery' ], WINP_Plugin::app()->getPluginVersion(), true ); | |
| 112 | -} | |
| 113 | - | |
| 114 | -add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_tinymce_assets' ); | |
| 115 | -add_action( 'admin_enqueue_scripts', 'wbcr_inp_enqueue_scripts' ); | |
| 116 | - | |
| 117 | -/** | |
| 118 | - * Adds js variable required for shortcodes. | |
| 119 | - * | |
| 120 | - * @since 1.1.0 | |
| 121 | - * @see before_wp_tiny_mce | |
| 122 | - */ | |
| 123 | -function wbcr_inp_tinymce_data( $hook ) { | |
| 124 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 125 | - return; | |
| 126 | - } | |
| 127 | - | |
| 128 | - // styles for the plugin shorcodes | |
| 129 | - $shortcode_icon = WINP_PLUGIN_URL . '/admin/assets/img/shortcode-icon5.png'; | |
| 130 | - $shortcode_title = __( 'Woody ad snippets', 'insert-php' ); | |
| 131 | - | |
| 132 | - $result = WINP_Helper::get_shortcode_data( true ); | |
| 133 | - $shortcode_snippets_json = json_encode( $result ); | |
| 134 | - ?> | |
| 135 | - <!-- <?php echo WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 136 | - <style> | |
| 137 | - i.wbcr-inp-shortcode-icon { | |
| 138 | - background: url("<?php echo $shortcode_icon ?>") center no-repeat; | |
| 139 | - } | |
| 140 | - </style> | |
| 141 | - <script> | |
| 142 | - var wbcr_inp_tinymce_snippets_button_title = '<?php echo $shortcode_title ?>'; | |
| 143 | - var wbcr_inp_post_tinymce_nonce = '<?php echo wp_create_nonce( 'wbcr_inp_tinymce_post_nonce' ) ?>'; | |
| 144 | - var wbcr_inp_shortcode_snippets = <?php echo $shortcode_snippets_json ?>; | |
| 145 | - </script> | |
| 146 | - <!-- /end <?php echo WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 147 | - <?php | |
| 148 | -} | |
| 149 | - | |
| 150 | -add_action( 'admin_print_scripts-post.php', 'wbcr_inp_tinymce_data' ); | |
| 151 | -add_action( 'admin_print_scripts-post-new.php', 'wbcr_inp_tinymce_data' ); | |
| 152 | -add_action( 'admin_print_scripts-widgets.php', 'wbcr_inp_tinymce_data' ); | |
| 153 | - | |
| 154 | -/** | |
| 155 | - * Deactivate snippet on trashed | |
| 156 | - * | |
| 157 | - * @since 2.0.6 | |
| 158 | - * | |
| 159 | - * @param $post_id | |
| 160 | - * | |
| 161 | - */ | |
| 162 | -function wbcr_inp_trash_post( $post_id ) { | |
| 163 | - $post_type = get_post_type( $post_id ); | |
| 164 | - if ( $post_type == WINP_SNIPPETS_POST_TYPE ) { | |
| 165 | - WINP_Helper::updateMetaOption( $post_id, 'snippet_activate', 0 ); | |
| 166 | - } | |
| 167 | -} | |
| 168 | - | |
| 169 | -add_action( 'wp_trash_post', 'wbcr_inp_trash_post' ); | |
| 170 | - | |
| 171 | -/** | |
| 172 | - * Removes the default 'new item' from the admin menu to add own page 'new item' later. | |
| 173 | - * | |
| 174 | - * @param $menu | |
| 175 | - * | |
| 176 | - * @return mixed | |
| 177 | - * @see menu_order | |
| 178 | - * | |
| 179 | - */ | |
| 180 | -function wbcr_inp_remove_new_item( $menu ) { | |
| 181 | - global $submenu; | |
| 182 | - | |
| 183 | - if ( ! isset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ] ) ) { | |
| 184 | - return $menu; | |
| 185 | - } | |
| 186 | - unset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ][10] ); | |
| 187 | - | |
| 188 | - return $menu; | |
| 189 | -} | |
| 190 | - | |
| 191 | -add_filter( 'custom_menu_order', '__return_true' ); | |
| 192 | -add_filter( 'admin_menu', 'wbcr_inp_remove_new_item' ); | |
| 193 | - | |
| 194 | -/** | |
| 195 | - * If the user tried to get access to the default 'new item', | |
| 196 | - * redirects forcibly to our page 'new item'. | |
| 197 | - * | |
| 198 | - * @see current_screen | |
| 199 | - */ | |
| 200 | -function wbcr_inp_redirect_to_new_item() { | |
| 201 | - $screen = get_current_screen(); | |
| 202 | - | |
| 203 | - if ( empty( $screen ) ) { | |
| 204 | - return; | |
| 205 | - } | |
| 206 | - if ( 'add' !== $screen->action || 'post' !== $screen->base || WINP_SNIPPETS_POST_TYPE !== $screen->post_type ) { | |
| 207 | - return; | |
| 208 | - } | |
| 209 | - | |
| 210 | - $winp_item = WINP_Plugin::app()->request->get( 'winp_item', null ); | |
| 211 | - if ( ! is_null( $winp_item ) ) { | |
| 212 | - return; | |
| 213 | - } | |
| 214 | - | |
| 215 | - $url = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=new-item-' . WINP_Plugin::app()->getPluginName() ); | |
| 216 | - | |
| 217 | - wp_safe_redirect( $url ); | |
| 218 | - | |
| 219 | - exit; | |
| 220 | -} | |
| 221 | - | |
| 222 | -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 | + * Reorder submenu items to place '+ Add Snippet' as second item | |
| 152 | + * | |
| 153 | + * @param array<int|string, mixed> $menu Menu items. | |
| 154 | + * | |
| 155 | + * @return array<int|string, mixed> | |
| 156 | + */ | |
| 157 | +function wbcr_inp_reorder_submenu_items( $menu ) { | |
| 158 | + global $submenu; | |
| 159 | + | |
| 160 | + if ( ! isset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ] ) ) { | |
| 161 | + return $menu; | |
| 162 | + } | |
| 163 | + | |
| 164 | + $snippet_submenu = $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ]; | |
| 165 | + $new_item_page = null; | |
| 166 | + $new_item_key = null; | |
| 167 | + | |
| 168 | + foreach ( $snippet_submenu as $key => $item ) { | |
| 169 | + if ( strpos( $item[2], 'new-item-' ) !== false ) { | |
| 170 | + $new_item_page = $item; | |
| 171 | + $new_item_key = $key; | |
| 172 | + break; | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + if ( null !== $new_item_page ) { | |
| 177 | + unset( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ][ $new_item_key ] ); | |
| 178 | + $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ][6] = $new_item_page; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 179 | + ksort( $submenu[ 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ] ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + return $menu; | |
| 183 | +} | |
| 184 | + | |
| 185 | +add_filter( 'admin_menu', 'wbcr_inp_reorder_submenu_items', 999 ); | |
| 186 | + | |
| 187 | +/** | |
| 188 | + * If the user tried to get access to the default 'new item', | |
| 189 | + * redirects forcibly to our page 'new item'. | |
| 190 | + * | |
| 191 | + * @see current_screen | |
| 192 | + */ | |
| 193 | +function wbcr_inp_redirect_to_new_item() { | |
| 194 | + $screen = get_current_screen(); | |
| 195 | + | |
| 196 | + if ( empty( $screen ) ) { | |
| 197 | + return; | |
| 198 | + } | |
| 199 | + if ( 'add' !== $screen->action || 'post' !== $screen->base || WINP_SNIPPETS_POST_TYPE !== $screen->post_type ) { | |
| 200 | + return; | |
| 201 | + } | |
| 202 | + | |
| 203 | + $winp_item = WINP_HTTP::get( 'winp_item', null ); | |
| 204 | + if ( ! is_null( $winp_item ) ) { | |
| 205 | + return; | |
| 206 | + } | |
| 207 | + | |
| 208 | + $url = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=winp-new-item' ); | |
| 209 | + | |
| 210 | + wp_safe_redirect( $url ); | |
| 211 | + | |
| 212 | + exit; | |
| 213 | +} | |
| 214 | + | |
| 215 | +add_action( 'current_screen', 'wbcr_inp_redirect_to_new_item' ); | |