'menubar,advlist,menubar_block,merge_toolbars', 'plugins' => 'anchor,code,insertdatetime,nonbreaking,print,searchreplace,table,visualblocks,visualchars,advlist,wptadv', 'toolbar_1' => 'formatselect,bold,italic,blockquote,bullist,numlist,alignleft,aligncenter,alignright,link,unlink,undo,redo', 'toolbar_2' => 'fontselect,fontsizeselect,outdent,indent,pastetext,removeformat,charmap,wp_more,forecolor,table,wp_help', 'toolbar_3' => '', 'toolbar_4' => '', 'toolbar_classic_block' => 'formatselect,bold,italic,blockquote,bullist,numlist,alignleft,aligncenter,alignright,' . 'link,forecolor,backcolor,table,wp_help', ); } private function get_default_admin_settings() { return array( 'options' => 'classic_paragraph_block,table_resize_bars,table_grid,table_tab_navigation,table_advtab', ); } private function get_all_plugins() { return array( 'advlist', 'anchor', 'code', 'contextmenu', 'emoticons', 'importcss', 'insertdatetime', 'link', 'nonbreaking', 'print', 'searchreplace', 'table', 'visualblocks', 'visualchars', 'wptadv', ); } // Options are only "boolean"", array element exists or not. private function get_all_user_options() { return array( 'advlist', 'advlink', 'contextmenu', 'menubar', 'menubar_block', 'fontsize_formats', 'merge_toolbars', 'disable_richtext_buttons', ); } private function get_all_admin_options() { return array( 'importcss', 'no_autop', 'hybrid_mode', 'classic_paragraph_block', 'replace_block_editor', 'table_resize_bars', 'table_default_attributes', 'table_grid', 'table_tab_navigation', 'table_advtab', ); } private function get_editor_locations() { return array( 'edit_post_screen', 'rest_of_wpadmin', 'on_front_end', ); } public function __construct() { if ( is_admin() ) { add_action( 'admin_menu', array( $this, 'add_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 10, 2 ); add_action( 'before_wp_tiny_mce', array( $this, 'show_version_warning' ) ); add_action( 'admin_init', array( $this, 'import_export_settings_file' ) ); add_action( 'plugins_loaded', array( $this, 'update_settings' ) ); } add_filter( 'wp_editor_settings', array( $this, 'disable_for_editor' ), 10, 2 ); add_filter( 'mce_buttons', array( $this, 'mce_buttons_1' ), 999, 2 ); add_filter( 'mce_buttons_2', array( $this, 'mce_buttons_2' ), 999, 2 ); add_filter( 'mce_buttons_3', array( $this, 'mce_buttons_3' ), 999, 2 ); add_filter( 'mce_buttons_4', array( $this, 'mce_buttons_4' ), 999, 2 ); add_filter( 'tiny_mce_before_init', array( $this, 'mce_options' ), 10, 2 ); add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins' ), 999 ); add_filter( 'tiny_mce_plugins', array( $this, 'tiny_mce_plugins' ), 999 ); add_action( 'enqueue_block_editor_assets', array( $this, 'block_editor_assets' ), 20 ); add_action( 'init', array( $this, 'block_editor_init' ) ); add_filter( 'wp_insert_post_data', array( $this, 'filter_post_content' ), 1 ); add_filter( 'excerpt_allowed_blocks', array( $this, 'excerpt_add_allowed_blocks' ) ); } public function disable_for_editor( $settings, $editor_id ) { static $editor_style_added = false; if ( empty( $this->admin_settings ) ) { $this->load_settings(); } $this->disabled_for_editor = false; $this->editor_id = $editor_id; if ( ! empty( $this->admin_settings['disabled_editors'] ) ) { $disabled_editors = explode( ',', $this->admin_settings['disabled_editors'] ); $current_screen = isset( $GLOBALS['current_screen'] ) ? $GLOBALS['current_screen'] : new stdClass; if ( is_admin() ) { if ( $editor_id === 'content' && ( $current_screen->id === 'post' || $current_screen->id === 'page' ) ) { if ( in_array( 'edit_post_screen', $disabled_editors, true ) ) { $this->disabled_for_editor = true; } } elseif ( in_array( 'rest_of_wpadmin', $disabled_editors, true ) ) { $this->disabled_for_editor = true; } } elseif ( in_array( 'on_front_end', $disabled_editors, true ) ) { $this->disabled_for_editor = true; } } if ( ! $this->disabled_for_editor && ! $editor_style_added ) { if ( $this->check_admin_setting( 'importcss' ) && $this->has_editor_style() === false ) { add_editor_style(); } $editor_style_added = true; } return $settings; } private function is_disabled() { return $this->disabled_for_editor; } private function has_editor_style() { if ( ! current_theme_supports( 'editor-style' ) ) { return false; } $editor_stylesheets = get_editor_stylesheets(); if ( is_array( $editor_stylesheets ) ) { foreach ( $editor_stylesheets as $url ) { if ( strpos( $url, 'editor-style.css' ) !== false ) { return $url; } } } return ''; } public function load_textdomain() { load_plugin_textdomain( 'tinymce-advanced', false, 'tinymce-advanced/langs' ); } public function enqueue_scripts( $page ) { if ( 'settings_page_tinymce-advanced' === $page ) { $plugin_url = plugins_url( 'plugin-assets', __FILE__ ); wp_enqueue_style( 'tadv-css', $plugin_url . '/tadv.css', array( 'editor-buttons' ), $this->plugin_version ); wp_enqueue_script( 'tadv-js', $plugin_url . '/tadv.js', array( 'jquery-ui-sortable' ), $this->plugin_version, true ); wp_enqueue_style( 'tadv-mce-skin', includes_url( 'js/tinymce/skins/lightgray/skin.min.css' ), array(), $this->plugin_version ); add_action( 'admin_footer', array( $this, 'load_mce_translation' ) ); } } public function load_mce_translation() { if ( ! class_exists( '_WP_Editors' ) ) { require( ABSPATH . WPINC . '/class-wp-editor.php' ); } ?> admin_settings ) ) { $this->admin_settings = get_option( 'tadv_admin_settings', false ); } if ( empty( $this->user_settings ) ) { $this->user_settings = get_option( 'tadv_settings', false ); } // load defaults if the options don't exist... if ( $this->admin_settings === false ) { $this->admin_settings = $this->get_default_admin_settings(); } $this->admin_options = ! empty( $this->admin_settings['options'] ) ? explode( ',', $this->admin_settings['options'] ) : array(); $default_user_settings = $this->get_default_user_settings(); if ( $this->user_settings === false ) { $this->user_settings = $default_user_settings; } if ( empty( $this->user_settings['toolbar_1'] ) ) { $this->user_settings['toolbar_1'] = $default_user_settings['toolbar_1']; } if ( empty( $this->user_settings['toolbar_classic_block'] ) ) { $this->user_settings['toolbar_classic_block'] = $default_user_settings['toolbar_classic_block']; } $this->options = ! empty( $this->user_settings['options'] ) ? explode( ',', $this->user_settings['options'] ) : array(); $this->plugins = ! empty( $this->user_settings['plugins'] ) ? explode( ',', $this->user_settings['plugins'] ) : array(); $this->toolbar_1 = ! empty( $this->user_settings['toolbar_1'] ) ? explode( ',', $this->user_settings['toolbar_1'] ) : array(); $this->toolbar_2 = ! empty( $this->user_settings['toolbar_2'] ) ? explode( ',', $this->user_settings['toolbar_2'] ) : array(); $this->toolbar_3 = ! empty( $this->user_settings['toolbar_3'] ) ? explode( ',', $this->user_settings['toolbar_3'] ) : array(); $this->toolbar_4 = ! empty( $this->user_settings['toolbar_4'] ) ? explode( ',', $this->user_settings['toolbar_4'] ) : array(); $this->toolbar_classic_block = ! empty( $this->user_settings['toolbar_classic_block'] ) ? explode( ',', $this->user_settings['toolbar_classic_block'] ) : array(); $this->used_buttons = array_merge( $this->toolbar_1, $this->toolbar_2, $this->toolbar_3, $this->toolbar_4, $this->toolbar_classic_block ); $this->get_all_buttons(); // Force refresh after activation. if ( ! empty( $GLOBALS['tinymce_version'] ) && strpos( $GLOBALS['tinymce_version'], '-tadv-' ) === false ) { $GLOBALS['tinymce_version'] .= '-tadv-' . $this->plugin_version; } } public function show_version_warning() { if ( is_admin() && current_user_can( 'update_plugins' ) && get_current_screen()->base === 'post' ) { $this->warn_if_unsupported(); } } public function warn_if_unsupported() { if ( ! $this->check_minimum_supported_version() ) { $wp_version = ! empty( $GLOBALS['wp_version'] ) ? $GLOBALS['wp_version'] : '(undefined)'; ?>
required_wp_version,
esc_html( $wp_version )
);
echo '
';
printf(
__( 'Please upgrade your WordPress installation or download an older version of the plugin.', 'tinymce-advanced' ),
'https://wordpress.org/plugins/tinymce-advanced/advanced/#download-previous-link'
);
?>
tags inside the classic block :-( // $data is slashed... if ( strpos( $content, '
' ) !== false ) { $content = str_replace( '
', '
', $content ); } $data['post_content'] = $content; return $data; } // Excerpts can be generated from classic paragraph blocks public function excerpt_add_allowed_blocks( $allowed_blocks ) { // Make sure a plugin doesn't pass the wrong type here... $allowed_blocks = (array) $allowed_blocks; $allowed_blocks[] = 'tadv/classic-paragraph'; return $allowed_blocks; } public function mce_external_plugins( $mce_plugins ) { if ( ! is_array( $this->options ) ) { $this->load_settings(); } if ( $this->is_disabled() ) { return $mce_plugins; } if ( ! is_array( $this->plugins ) ) { $this->plugins = array(); } $this->plugins[] = 'wptadv'; if ( $this->check_user_setting( 'menubar' ) || $this->check_user_setting( 'menubar_block' ) ) { $this->plugins = array_merge( $this->plugins, $this->required_menubar_plugins ); } $this->plugins = array_intersect( $this->plugins, $this->get_all_plugins() ); $plugin_url = plugins_url( 'mce/', __FILE__ ); $mce_plugins = (array) $mce_plugins; $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; foreach ( $this->plugins as $plugin ) { $mce_plugins["$plugin"] = $plugin_url . $plugin . "/plugin{$suffix}.js"; } return $mce_plugins; } public function tiny_mce_plugins( $plugins ) { if ( $this->is_disabled() ) { return $plugins; } if ( in_array( 'image', $this->used_buttons, true ) && ! in_array( 'image', $plugins, true ) ) { $plugins[] = 'image'; } if ( ( in_array( 'rtl', $this->used_buttons, true ) || in_array( 'ltr', $this->used_buttons, true ) ) && ! in_array( 'directionality', (array) $plugins, true ) ) { $plugins[] = 'directionality'; } return $plugins; } private function parse_buttons( $toolbar_id = false, $buttons = false ) { if ( $toolbar_id && ! $buttons && ! empty( $_POST[$toolbar_id] ) ) $buttons = $_POST[$toolbar_id]; if ( is_array( $buttons ) ) { $_buttons = array_map( array( @$this, 'filter_name' ), $buttons ); return implode( ',', array_filter( $_buttons ) ); } return ''; } private function filter_name( $str ) { if ( empty( $str ) || ! is_string( $str ) ) return ''; // Button names return preg_replace( '/[^a-z0-9_]/i', '', $str ); } private function sanitize_settings( $settings ) { $_settings = array(); if ( ! is_array( $settings ) ) { return $_settings; } foreach( $settings as $name => $value ) { $name = preg_replace( '/[^a-z0-9_]+/', '', $name ); if ( strpos( $name, 'toolbar_' ) === 0 ) { $_settings[$name] = $this->parse_buttons( false, explode( ',', $value ) ); } else if ( 'options' === $name || 'plugins' === $name || 'disabled_plugins' === $name ) { $_settings[$name] = preg_replace( '/[^a-z0-9_,]+/', '', $value ); } } return $_settings; } /** * Validare array of settings against a whitelist. * * @param array $settings The settings. * @param array $checklist The whitelist. * @return string The validated settings CSV. */ private function validate_settings( $settings, $checklist ) { if ( empty( $settings ) ) { return ''; } elseif ( is_string( $settings ) ) { $settings = explode( ',', $settings ); } elseif ( ! is_array( $settings ) ) { return ''; } $_settings = array(); foreach ( $settings as $value ) { if ( in_array( $value, $checklist, true ) ) { $_settings[] = $value; } } return implode( ',', $_settings ); } private function save_settings( $all_settings = null ) { $settings = $user_settings = array(); $default_settings = $this->get_default_user_settings(); if ( empty( $this->buttons_filter ) ) { $this->get_all_buttons(); } if ( ! empty( $all_settings['settings'] ) ) { $user_settings = $all_settings['settings']; } for ( $i = 1; $i < 6; $i++ ) { $toolbar_name = ( $i < 5 ) ? 'toolbar_' . $i : 'toolbar_classic_block'; if ( ! empty( $user_settings[ $toolbar_name ] ) ) { $toolbar = explode( ',', $user_settings[ $toolbar_name ] ); } elseif ( ! empty( $_POST[ $toolbar_name ] ) && is_array( $_POST[ $toolbar_name ] ) ) { $toolbar = $_POST[ $toolbar_name ]; } else { $toolbar = array(); } if ( $i > 1 && in_array( 'wp_adv', $toolbar, true ) ) { $toolbar = array_diff( $toolbar, array( 'wp_adv' ) ); } $settings[ $toolbar_name ] = $this->validate_settings( $toolbar, $this->buttons_filter ); } if ( ! empty( $user_settings['options'] ) ) { $options = explode( ',', $user_settings['options'] ); } elseif ( ! empty( $_POST['options'] ) && is_array( $_POST['options'] ) ) { $options = $_POST['options']; if ( ! empty( $_POST['richtext_buttons'] ) && $_POST['richtext_buttons'] === 'no' ) { $options[] = 'disable_richtext_buttons'; } } else { $options = array(); } $settings['options'] = $this->validate_settings( $options, $this->get_all_user_options() ); if ( ! empty( $user_settings['plugins'] ) ) { $plugins = explode( ',', $user_settings['plugins'] ); } else { $plugins = array(); } if ( ! empty( $settings['options']['menubar'] ) || ! empty( $settings['options']['menubar_block'] ) ) { $plugins = array_merge( $plugins, $this->required_menubar_plugins ); } // Merge the submitted plugins with plugins needed for the buttons. $this->user_settings = $settings; $this->load_settings(); $plugins = $this->get_plugins( $plugins ); $settings['plugins'] = $this->validate_settings( $plugins, $this->get_all_plugins() ); $this->user_settings = $settings; $this->load_settings(); // Save the new settings. update_option( 'tadv_settings', $settings ); if ( ! is_multisite() || current_user_can( 'manage_sites' ) ) { $this->save_admin_settings( $all_settings ); } } private function save_admin_settings( $all_settings = null ) { $admin_settings = $save_admin_settings = array(); if ( ! empty( $all_settings['admin_settings'] ) ) { $admin_settings = $all_settings['admin_settings']; } if ( ! empty( $admin_settings ) ) { if ( ! empty( $admin_settings['options'] ) ) { $save_admin_settings['options'] = $this->validate_settings( $admin_settings['options'], $this->get_all_admin_options() ); } else { $save_admin_settings['options'] = ''; } $disabled_editors = array_intersect( $this->get_editor_locations(), explode( ',', $admin_settings['disabled_editors'] ) ); } elseif ( isset( $_POST['tadv-save'] ) ) { if ( ! empty( $_POST['admin_options'] ) && is_array( $_POST['admin_options'] ) ) { $save_admin_settings['options'] = $this->validate_settings( $_POST['admin_options'], $this->get_all_admin_options() ); } if ( ! empty( $_POST['tadv_enable_at'] ) && is_array( $_POST['tadv_enable_at'] ) ) { $tadv_enable_at = $_POST['tadv_enable_at']; } else { $tadv_enable_at = array(); } $disabled_editors = array_diff( $this->get_editor_locations(), $tadv_enable_at ); } else { return; } $save_admin_settings['disabled_editors'] = implode( ',', $disabled_editors ); $this->admin_settings = $save_admin_settings; update_option( 'tadv_admin_settings', $save_admin_settings ); } private function import_from_file() { if ( empty( $_FILES['tadv-import']['name'] ) ) { return 1; } $file_type = wp_check_filetype( $_FILES['tadv-import']['name'], array( 'json' => 'application/json' ) ); if ( $file_type['ext'] !== 'json' ) { return 1; } $settings = @file_get_contents( $_FILES['tadv-import']['tmp_name'] ); if ( empty( $settings ) ) { return 2; } $settings = json_decode( $settings, true ); if ( ! is_array( $settings ) ) { return 3; } $this->save_settings( $settings ); return 0; } public function import_export_settings_file() { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( isset( $_POST['tadv-import-file'] ) && isset( $_POST['tadv-import-settings-nonce'] ) && wp_verify_nonce( $_POST['tadv-import-settings-nonce'], 'tadv-import-settings' ) ) { $err = $this->import_from_file(); $url = admin_url( 'options-general.php?page=tinymce-advanced' ); $url = add_query_arg( 'tadv-import-file-complete', $err, $url ); wp_safe_redirect( $url ); exit; } elseif ( isset( $_POST['tadv-export-settings'] ) && isset( $_POST['tadv-export-settings-nonce'] ) && wp_verify_nonce( $_POST['tadv-export-settings-nonce'], 'tadv-export-settings' ) ) { $this->load_settings(); $output = array( 'settings' => $this->user_settings ); // TODO: only admin || SA $output['admin_settings'] = $this->admin_settings; $sitename = get_bloginfo( 'name' ); if ( mb_strlen( $sitename ) > 100 ) { $sitename = mb_substr( $sitename, 0, 100 ); } $sitename = preg_replace( '/[ \.-]+/', '-', $sitename ); $date = date( 'Y-m-d' ); $filename = sanitize_file_name( $sitename . '-TMA-settings-' . $date . '.json' ); nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( "Content-Disposition: attachment; filename=$filename" ); echo wp_json_encode( $output ); exit; } } public function settings_page() { if ( ! defined( 'TADV_ADMIN_PAGE' ) ) { define( 'TADV_ADMIN_PAGE', true ); } include_once( plugin_dir_path( __FILE__ ) . 'tadv_admin.php' ); } public function add_menu() { $page_title = __( 'Advanced Editor Tools', 'tinymce-advanced' ); $menu_item_label = __( 'Advanced Editor Tools', 'tinymce-advanced' ); add_options_page( $page_title, $menu_item_label, 'manage_options', 'tinymce-advanced', array( $this, 'settings_page' ) ); } /** * Add a link to the settings page */ public function add_settings_link( $links, $file ) { if ( strrpos( $file, '/tinymce-advanced.php' ) === ( strlen( $file ) - 21 ) && current_user_can( 'manage_options' ) ) { $settings_link = sprintf( '%s', admin_url( 'options-general.php?page=tinymce-advanced' ), __( 'Settings', 'tinymce-advanced' ) ); $links = (array) $links; $links['tma_settings_link'] = $settings_link; } return $links; } } new Advanced_Editor_Tools; endif;