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