css
5 years ago
font
5 years ago
images
5 years ago
js
5 years ago
admin.php
5 years ago
edit.php
5 years ago
form.php
5 years ago
insert.php
5 years ago
manage.php
5 years ago
tools.php
5 years ago
tools.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | class SC_Admin_Tools{ |
| 6 | |
| 7 | public static function init(){ |
| 8 | |
| 9 | // Register the action for admin ajax features |
| 10 | add_action( 'wp_ajax_sc_insert_window', array( __CLASS__, 'insert_window' ) ); |
| 11 | |
| 12 | // Add TinyMCE button |
| 13 | add_action( 'admin_init', array( __CLASS__, 'register_mce' ) ); |
| 14 | |
| 15 | add_action( 'wp_enqueue_editor', array( __CLASS__, 'enqueue_insert_scripts' ) ); |
| 16 | |
| 17 | // Gutenberg block |
| 18 | add_action( 'init', array( __CLASS__, 'register_block' ) ); |
| 19 | |
| 20 | } |
| 21 | |
| 22 | public static function register_mce(){ |
| 23 | |
| 24 | add_filter( 'mce_buttons', array( __CLASS__, 'register_mce_button' ) ); |
| 25 | |
| 26 | add_filter( 'mce_external_plugins', array( __CLASS__, 'register_mce_js' ) ); |
| 27 | |
| 28 | } |
| 29 | |
| 30 | public static function register_mce_button( $buttons ){ |
| 31 | |
| 32 | if( self::is_sc_edit_page() ) |
| 33 | return $buttons; |
| 34 | |
| 35 | array_push( $buttons, 'separator', 'shortcoder' ); |
| 36 | return $buttons; |
| 37 | |
| 38 | } |
| 39 | |
| 40 | public static function register_mce_js( $plugins ){ |
| 41 | |
| 42 | if( self::is_sc_edit_page() ) |
| 43 | return $plugins; |
| 44 | |
| 45 | $plugins[ 'shortcoder' ] = SC_ADMIN_URL . '/js/tinymce/editor_plugin.js'; |
| 46 | return $plugins; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | public static function register_block(){ |
| 51 | |
| 52 | if( !function_exists( 'register_block_type' ) ){ |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | wp_register_script( |
| 57 | 'shortcoder', SC_ADMIN_URL . '/js/blocks/shortcoder.js', array( 'wp-blocks', 'wp-element' ) |
| 58 | ); |
| 59 | |
| 60 | register_block_type( 'shortcoder/shortcoder', array( |
| 61 | 'editor_script' => 'shortcoder', |
| 62 | )); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | public static function enqueue_insert_scripts(){ |
| 67 | |
| 68 | if( self::is_sc_edit_page() || !is_admin() ) |
| 69 | return; |
| 70 | |
| 71 | wp_enqueue_script( 'sc-tools-js', SC_ADMIN_URL . 'js/script-tools.js', array( 'jquery' ), SC_VERSION ); |
| 72 | |
| 73 | wp_enqueue_style( 'sc-tools-css', SC_ADMIN_URL . 'css/style-tools.css', array(), SC_VERSION ); |
| 74 | |
| 75 | wp_localize_script( 'sc-tools-js', 'SC_INSERT_VARS', array( |
| 76 | 'insert_page' => admin_url( 'admin-ajax.php?action=sc_insert_window' ), |
| 77 | 'popup_title' => __( 'Insert shortcode to editor', 'shortcoder' ), |
| 78 | 'popup_opened' => false, |
| 79 | 'block_editor' => false |
| 80 | )); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | public static function insert_window(){ |
| 85 | |
| 86 | include_once( 'insert.php' ); |
| 87 | die(0); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | public static function is_sc_edit_page(){ |
| 92 | |
| 93 | if( !is_admin() ){ |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | $screen = get_current_screen(); |
| 98 | return ( $screen->post_type == SC_POST_TYPE && $screen->base == 'post' ); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | SC_Admin_Tools::init(); |
| 105 | |
| 106 | ?> |