| @@ -1,178 +1,210 @@ | ||
| 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' ); | |
| 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 | + * 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 | + } | |
| 17 | + | |
| 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)); | |
| 20 | + | |
| 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>'; | |
| 24 | + | |
| 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 | + $create_notice_url = admin_url('edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE); | |
| 35 | + | |
| 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'); | |
| 38 | + | |
| 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'); | |
| 40 | + | |
| 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>'; | |
| 42 | + | |
| 43 | + $upgrade_plugin_notice .= '<br><br><a href="' . $create_notice_url . '" class="button button-default">' . __('Create new php snippet', 'insert-php') . '</a> '; | |
| 44 | + | |
| 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'); | |
| 48 | + | |
| 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 | + /** | |
| 58 | + * Show error notification after saving snippet. We can also show this message when the snippet is activated. | |
| 59 | + * We must warn the user that we can not perform the spippet due to an error. | |
| 60 | + */ | |
| 61 | + if( isset($_GET['wbcr_inp_save_snippet_result']) && $_GET['wbcr_inp_save_snippet_result'] == 'code-error' ) { | |
| 62 | + | |
| 63 | + $post_id = isset($_GET['post']) | |
| 64 | + ? intval($_GET['post']) | |
| 65 | + : null; | |
| 66 | + | |
| 67 | + if( $post_id && $error = WINP_Plugin::app()->getSnippetError($post_id) ) { | |
| 68 | + | |
| 69 | + $error_message = sprintf('<p>%s</p><p><strong>%s</strong></p>', sprintf(__('The snippet has been deactivated due to an error on line %d:', 'insert-php'), $error['line']), $error['message']); | |
| 70 | + | |
| 71 | + $notices[] = array( | |
| 72 | + 'id' => 'inp_result_error', | |
| 73 | + 'where' => array('post', 'post-new', 'edit'), | |
| 74 | + 'type' => 'error', | |
| 75 | + 'dismissible' => false, | |
| 76 | + 'dismiss_expires' => 0, | |
| 77 | + 'text' => $error_message | |
| 78 | + ); | |
| 79 | + } | |
| 80 | + } | |
| 81 | + | |
| 82 | + return $notices; | |
| 83 | + } | |
| 84 | + | |
| 85 | + add_filter('wbcr_factory_admin_notices', 'wbcr_inp_admin_conflict_notices_error', 10, 2); | |
| 86 | + | |
| 87 | + function wbcr_inp_admin_init() | |
| 88 | + { | |
| 89 | + $plugin = WINP_Plugin::app(); | |
| 90 | + | |
| 91 | + // Disable safe mode | |
| 92 | + | |
| 93 | + if( isset($_COOKIE['wbcr-php-snippets-safe-mode']) && isset($_REQUEST['wbcr-php-snippets-disable-safe-mode']) && $plugin->currentUserCan() ) { | |
| 94 | + unset($_COOKIE['wbcr-php-snippets-safe-mode']); | |
| 95 | + setcookie('wbcr-php-snippets-safe-mode', null, -1); | |
| 96 | + wp_safe_redirect(remove_query_arg(array('wbcr-php-snippets-disable-safe-mode'))); | |
| 97 | + exit; | |
| 98 | + } | |
| 99 | + | |
| 100 | + // Register metaboxes | |
| 101 | + | |
| 102 | + require_once(WINP_PLUGIN_DIR . '/admin/metaboxes/base-options.php'); | |
| 103 | + Wbcr_FactoryMetaboxes403::registerFor(new WINP_BaseOptionsMetaBox($plugin), WINP_SNIPPETS_POST_TYPE, $plugin); | |
| 104 | + | |
| 105 | + require_once(WINP_PLUGIN_DIR . '/admin/metaboxes/info.php'); | |
| 106 | + Wbcr_FactoryMetaboxes403::registerFor(new WINP_InfoMetaBox($plugin), WINP_SNIPPETS_POST_TYPE, $plugin); | |
| 107 | + | |
| 108 | + // Upgrade up to new version | |
| 109 | + if( !$plugin->getOption('upgrade_up_to_201', false) ) { | |
| 110 | + $role = get_role('administrator'); | |
| 111 | + if( !$role ) { | |
| 112 | + return; | |
| 113 | + } | |
| 114 | + | |
| 115 | + $role->add_cap('edit_' . WINP_SNIPPETS_POST_TYPE); | |
| 116 | + $role->add_cap('read_' . WINP_SNIPPETS_POST_TYPE); | |
| 117 | + $role->add_cap('delete_' . WINP_SNIPPETS_POST_TYPE); | |
| 118 | + $role->add_cap('edit_' . WINP_SNIPPETS_POST_TYPE . 's'); | |
| 119 | + $role->add_cap('edit_others_' . WINP_SNIPPETS_POST_TYPE . 's'); | |
| 120 | + $role->add_cap('publish_' . WINP_SNIPPETS_POST_TYPE . 's'); | |
| 121 | + $role->add_cap('read_private_' . WINP_SNIPPETS_POST_TYPE . 's'); | |
| 122 | + | |
| 123 | + $plugin->updateOption('upgrade_up_to_201', 1); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + add_action('admin_init', 'wbcr_inp_admin_init'); | |
| 128 | + | |
| 129 | + // --- | |
| 130 | + // Editor | |
| 131 | + // | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * Asset scripts for the tinymce editor | |
| 135 | + * | |
| 136 | + * @param string $hook | |
| 137 | + */ | |
| 138 | + function wbcr_inp_enqueue_tinymce_assets($hook) | |
| 139 | + { | |
| 140 | + $pages = array( | |
| 141 | + 'post.php', | |
| 142 | + 'post-new.php', | |
| 143 | + 'widgets.php' | |
| 144 | + ); | |
| 145 | + | |
| 146 | + if( !in_array($hook, $pages) || !current_user_can('manage_options') ) { | |
| 147 | + return; | |
| 148 | + } | |
| 149 | + | |
| 150 | + wp_enqueue_script('wbcr-inp-tinymce-button-widget', WINP_PLUGIN_URL . '/admin/assets/js/tinymce4.4.js', array('jquery'), WINP_Plugin::app() | |
| 151 | + ->getPluginVersion(), true); | |
| 152 | + } | |
| 153 | + | |
| 154 | + add_action('admin_enqueue_scripts', 'wbcr_inp_enqueue_tinymce_assets'); | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Adds js variable required for shortcodes. | |
| 158 | + * | |
| 159 | + * @see before_wp_tiny_mce | |
| 160 | + * @since 1.1.0 | |
| 161 | + */ | |
| 162 | + function wbcr_inp_tinymce_data($hook) | |
| 163 | + { | |
| 164 | + if( !current_user_can('manage_options') ) { | |
| 165 | + return; | |
| 166 | + } | |
| 167 | + | |
| 168 | + // styles for the plugin shorcodes | |
| 169 | + $shortcode_icon = WINP_PLUGIN_URL . '/admin/assets/img/shortcode-icon.png'; | |
| 170 | + $shortcode_title = __('PHP snippets', 'insert-php'); | |
| 171 | + | |
| 172 | + $snippets = get_posts(array( | |
| 173 | + 'post_type' => WINP_SNIPPETS_POST_TYPE, | |
| 174 | + 'meta_key' => WINP_Plugin::app()->getPrefix() . 'snippet_scope', | |
| 175 | + 'meta_value' => 'shortcode', | |
| 176 | + 'post_status' => 'publish', | |
| 177 | + 'numberposts' => -1 | |
| 178 | + )); | |
| 179 | + | |
| 180 | + $result = array(); | |
| 181 | + foreach((array)$snippets as $snippet) { | |
| 182 | + $result[] = array( | |
| 183 | + 'id' => $snippet->ID, | |
| 184 | + 'title' => empty($snippet->post_title) | |
| 185 | + ? '(no titled, ID=' . $snippet->ID . ')' | |
| 186 | + : $snippet->post_title | |
| 187 | + ); | |
| 188 | + } | |
| 189 | + | |
| 190 | + $shortcode_snippets_json = json_encode($result); | |
| 191 | + | |
| 192 | + ?> | |
| 193 | + <!-- <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 194 | + <style> | |
| 195 | + i.wbcr-inp-shortcode-icon { | |
| 196 | + background: url("<?php echo $shortcode_icon ?>") center no-repeat; | |
| 197 | + } | |
| 198 | + </style> | |
| 199 | + <script> | |
| 200 | + var wbcr_inp_tinymce_snippets_button_title = '<?php echo $shortcode_title ?>'; | |
| 201 | + var wbcr_inp_post_tinymce_nonce = '<?php echo wp_create_nonce('wbcr_inp_tinymce_post_nonce') ?>'; | |
| 202 | + var wbcr_inp_shortcode_snippets = <?= $shortcode_snippets_json ?>; | |
| 203 | + </script> | |
| 204 | + <!-- /end <?= WINP_Plugin::app()->getPluginTitle() ?> for tinymce --> | |
| 205 | + <?php | |
| 206 | + } | |
| 207 | + | |
| 208 | + add_action('admin_print_scripts-post.php', 'wbcr_inp_tinymce_data'); | |
| 209 | + add_action('admin_print_scripts-post-new.php', 'wbcr_inp_tinymce_data'); | |
| 210 | + add_action('admin_print_scripts-widgets.php', 'wbcr_inp_tinymce_data'); | |