*/
class Admin_Code_Editor_Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $admin_code_editor The ID of this plugin.
*/
private $admin_code_editor;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $admin_code_editor The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $admin_code_editor, $version ) {
$this->admin_code_editor = $admin_code_editor;
$this->version = $version;
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles() {
wp_enqueue_style( $this->admin_code_editor,
plugin_dir_url( __FILE__ ) . 'css/admin-code-editor-admin.css',
array(),
filemtime(plugin_dir_path( __FILE__ ) . 'css/admin-code-editor-admin.css'),
'all'
);
wp_enqueue_style(
'wp-ace-bootstrap',
plugin_dir_url( __FILE__ ) . 'css/wp-ace-bootstrap.css',
array(),
filemtime(plugin_dir_path( __FILE__ ) . 'css/wp-ace-bootstrap.css'),
'all'
);
wp_enqueue_style(
'wp-ace-bootstrap-theme',
plugin_dir_url( __FILE__ ) . 'css/wp-ace-bootstrap-theme.css',
array('wp-ace-bootstrap'),
filemtime(plugin_dir_path( __FILE__ ) . 'css/wp-ace-bootstrap-theme.css'),
'all'
);
wp_enqueue_style(
'wp-ace-font-awesome',
'//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css',
array(),
'4.6.3',
'all'
);
wp_enqueue_style(
'wp-ace-jquery-ui',
'//code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css',
array(),
'1.11.4',
'all'
);
}
/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*/
public function enqueue_scripts($hook) {
global $post;
if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
$selected_post_types = get_option('wp_ace_enabled_post_type');
if ( in_array($post->post_type, $selected_post_types)) {
wp_enqueue_script( 'jquery-ui-core' );
wp_enqueue_script( 'jquery-ui-resizable' );
wp_enqueue_script( 'backbone' );
wp_enqueue_script( 'underscore' );
wp_enqueue_script(
'wp-ace-editor-js',
plugin_dir_url( __FILE__ ) . 'js/ace-src-min-noconflict/ace.js',
array('jquery'),
filemtime(plugin_dir_path( __FILE__ ) . 'js/ace-src-min-noconflict/ace.js')
);
wp_enqueue_script(
'wp-ace-bootstrap-js',
plugin_dir_url( __FILE__ ) . 'js/bootstrap.min.js' ,
array('jquery'),
filemtime(plugin_dir_path( __FILE__ ) . 'js/bootstrap.min.js')
);
wp_enqueue_script(
$this->admin_code_editor,
plugin_dir_url( __FILE__ ) . 'js/admin-code-editor-admin.js',
array( 'jquery', 'wp-ace-bootstrap-js', 'wp-ace-editor-js', 'jquery-ui-resizable', 'underscore', 'backbone' ),
filemtime(plugin_dir_path( __FILE__ ) . 'js/admin-code-editor-admin.js')
);
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-html-php.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-css.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-js.php';
$editor_args = array(
'type' => 'html-php',
'host-post-id' => $post->ID
);
$html_php_editor = new Admin_Code_Editor_Editor_HTML_PHP($editor_args);
$editor_args = array(
'type' => 'css',
'host-post-id' => $post->ID
);
$css_editor = new Admin_Code_Editor_Editor_CSS($editor_args);
$editor_args = array(
'type' => 'js',
'host-post-id' => $post->ID
);
$js_editor = new Admin_Code_Editor_Editor_JS($editor_args);
$post_type_obj = get_post_type_object( $post->post_type );
$wpcr_data = array(
'wp-ace-html-php-code-position' => $html_php_editor->get_code_output_position(),
'wp-ace-html-php-preprocessor' => $html_php_editor->get_preprocessor(),
'wp-ace-css-preprocessor' => $css_editor->get_preprocessor(),
'wp-ace-css-include-jquery' => $js_editor->get_include_jquery_status(),
'wp-ace-js-preprocessor' => $js_editor->get_preprocessor(),
'wp-ace-post-type-singular-name' => $post_type_obj->labels->singular_name,
'wp-ace-html-php-compile-status' => $html_php_editor->get_code_compile_status(),
'wp-ace-css-compile-status' => $css_editor->get_code_compile_status(),
'wp-ace-js-compile-status' => $js_editor->get_code_compile_status()
);
wp_localize_script( $this->admin_code_editor, 'wpcr_data', $wpcr_data);
}
}
}
function code_editor_add_meta_box() {
$selected_post_types = get_option('wp_ace_enabled_post_type');
$screens = $selected_post_types;
foreach ( $screens as $screen ) {
add_meta_box(
'code_box',
__( 'Admin Code Editor', 'wp-ace-editor' ),
array(&$this,'code_editor_section_callback'),
$screen,
'normal',
'high'
);
}
}
// Display WordPress error message if at least one of the HTML, CSS, or JS precode compilation contains an error
public function admin_post_error_notice() {
global $post, $pagenow;
$screen = get_current_screen();
if ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) {
$selected_post_types = get_option('wp_ace_enabled_post_type');
if ( in_array($post->post_type, $selected_post_types)) {
$editor_args = array(
'type' => 'html-php',
'host-post-id' => $post->ID
);
$html_php_editor = new Admin_Code_Editor_Editor_HTML_PHP($editor_args);
$editor_args = array(
'type' => 'css',
'host-post-id' => $post->ID
);
$css_editor = new Admin_Code_Editor_Editor_CSS($editor_args);
$editor_args = array(
'type' => 'js',
'host-post-id' => $post->ID
);
$js_editor = new Admin_Code_Editor_Editor_JS($editor_args);
if (
$html_php_editor->get_code_compile_status() == 'error' ||
$css_editor->get_code_compile_status() == 'error' ||
$js_editor->get_code_compile_status() == 'error'
) {
?>
See below for more information.', 'wrs-admin-code-editor'); ?>
ID);
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-html-php.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-css.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-js.php';
$editor_args = array(
'type' => 'html-php',
'host-post-id' => $post->ID
);
$html_php_editor = new Admin_Code_Editor_Editor_HTML_PHP($editor_args);
$editor_args = array(
'type' => 'css',
'host-post-id' => $post->ID
);
$css_editor = new Admin_Code_Editor_Editor_CSS($editor_args);
$editor_args = array(
'type' => 'js',
'host-post-id' => $post->ID
);
$js_editor = new Admin_Code_Editor_Editor_JS($editor_args);
$preprocessor_options = get_option('wp_ace_supported_preprocessors');
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/admin-code-editor-admin-post-edit.php';
}
/**
*
* Checks plugin version
* @since 1.0.0
*/
function plugin_update_check() {
if (get_site_option( 'wp_ace_plugin_version' ) != $this->version) {
$this->plugin_update();
}
}
/**
* Delete associated code posts (HTML, CSS, JS) when host post is deleted
* @param int $postid
* @since 1.0.0
*/
function delete_code_posts($postid) {
global $post_type;
$selected_post_types = get_option('wp_ace_enabled_post_type');
if ( !in_array($post_type, $selected_post_types)) {
return;
}
$html_code_post_id = get_post_meta($postid, '_wp_ace_html_php_code_post_id', true);
$css_code_post_id = get_post_meta($postid, '_wp_ace_css_code_post_id', true);
$js_code_post_id = get_post_meta($postid, '_wp_ace_js_code_post_id', true);
wp_delete_post( $html_code_post_id, true );
wp_delete_post( $css_code_post_id, true );
wp_delete_post( $js_code_post_id, true );
}
/**
* Set constant settings at plugin activation or update
* @since 1.0.0
*/
private function plugin_update() {
$supported_preprocessors = array(
'html' => array(
'haml' => 'HAML',
'markdown' => 'MarkDown'
),
'css' => array(
'scss' => 'SCSS',
'less' => 'LESS',
//'stylus' => 'Stylus'
),
'js' => array(
'coffee' => 'CoffeeScript'
)
);
update_option( 'wp_ace_supported_preprocessors', $supported_preprocessors);
update_option( 'wp_ace_plugin_version', $this->version);
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
* @since 1.0.0
*/
function code_editor_save( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['wp-ace-editor-nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['wp-ace-editor-nonce'], 'wp-ace-editor-nonce' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( false !== wp_is_post_revision( $post_id ) )
return;
$wp_ace_code_content_types = array("wp-ace-html", "wp-ace-css", "wp-ace-js");
$post_type = get_post_type( $post_id );
if ( in_array(get_post_type( $post_id ), $wp_ace_code_content_types) ) {
// since wp_insert_post also calls code_editor_save, we need to check if this is a code content type and exit.
// An infinite loop will occur otherwise.
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-admin-code-editor-general.php';
$general_settings = new Admin_Code_Editor_General($post_id);
$general_settings->updateDataFromPOST();
if (!$general_settings->htmlEditorIsDisabled()) {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-html-php.php';
$editor_args = array(
'type' => 'html-php',
'host-post-id' => $post_id
);
$html_editor = new Admin_Code_Editor_Editor_HTML_PHP($editor_args);
$html_editor->initialize_from_post_request();
$html_editor->update_code();
}
if (!$general_settings->cssEditorIsDisabled()) {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-css.php';
$editor_args = array(
'type' => 'css',
'host-post-id' => $post_id
);
$css_editor = new Admin_Code_Editor_Editor_CSS($editor_args);
$css_editor->initialize_from_post_request();
$css_editor->update_code();
}
if (!$general_settings->jsEditorIsDisabled()) {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-admin-code-editor-editor-js.php';
$editor_args = array(
'type' => 'js',
'host-post-id' => $post_id
);
$js_editor = new Admin_Code_Editor_Editor_JS($editor_args);
$js_editor->initialize_from_post_request();
$js_editor->update_code();
}
}
/**
* Create the code post types for the host post
*
* @since 1.0.0
*/
function wp_ace_post_type_init() {
$labels = array(
'name' => _x( 'Admin Code Editor Pre HTML', 'post type general name', 'admin-code-editor' ),
'singular_name' => _x( 'Admin Code Editor Pre HTML', 'post type singular name', 'admin-code-editor' ),
'menu_name' => _x( 'Admin Code Editor Pre HTML', 'admin menu', 'admin-code-editor' ),
'name_admin_bar' => _x( 'Admin Code Editor Pre HTML', 'add new on admin bar', 'admin-code-editor' ),
'add_new' => _x( 'Add New', 'nw-item', 'admin-code-editor' ),
'add_new_item' => __( 'Add New Admin Code Editor Pre HTML', 'admin-code-editor' ),
'new_item' => __( 'New Admin Code Editor Pre HTML', 'admin-code-editor' ),
'edit_item' => __( 'Edit Admin Code Editor Pre HTML', 'admin-code-editor' ),
'view_item' => __( 'View Admin Code Editor Pre HTML', 'admin-code-editor' ),
'all_items' => __( 'All Admin Code Editor Pre HTML', 'admin-code-editor' ),
'search_items' => __( 'Search Admin Code Editor Pre HTML', 'admin-code-editor' ),
'parent_item_colon' => __( 'Parent Admin Code Editor Pre HTML:', 'admin-code-editor' ),
'not_found' => __( 'No Admin Code Editor Pre HTML found.', 'admin-code-editor' ),
'not_found_in_trash' => __( 'No Admin Code Editor Pre HTML found in Trash.', 'admin-code-editor' )
);
$args = array(
'labels' => $labels,
'public' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'editor', 'author', 'revisions')
);
register_post_type('wp-ace-html',$args);
$labels = array(
'name' => _x( 'Admin Code Editor Pre CSS', 'post type general name', 'admin-code-editor' ),
'singular_name' => _x( 'Admin Code Editor Pre CSS', 'post type singular name', 'admin-code-editor' ),
'menu_name' => _x( 'Admin Code Editor Pre CSS', 'admin menu', 'admin-code-editor' ),
'name_admin_bar' => _x( 'Admin Code Editor Pre CSS', 'add new on admin bar', 'admin-code-editor' ),
'add_new' => _x( 'Add New', 'nw-item', 'admin-code-editor' ),
'add_new_item' => __( 'Add New Admin Code Editor Pre CSS', 'admin-code-editor' ),
'new_item' => __( 'New Admin Code Editor Pre CSS', 'admin-code-editor' ),
'edit_item' => __( 'Edit Admin Code Editor Pre CSS', 'admin-code-editor' ),
'view_item' => __( 'View Admin Code Editor Pre CSS', 'admin-code-editor' ),
'all_items' => __( 'All Admin Code Editor Pre CSS', 'admin-code-editor' ),
'search_items' => __( 'Search Admin Code Editor Pre CSS', 'admin-code-editor' ),
'parent_item_colon' => __( 'Parent Admin Code Editor Pre CSS:', 'admin-code-editor' ),
'not_found' => __( 'No Admin Code Editor Pre CSS found.', 'admin-code-editor' ),
'not_found_in_trash' => __( 'No Admin Code Editor Pre CSS found in Trash.', 'admin-code-editor' )
);
$args = array(
'labels' => $labels,
'public' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'editor', 'author', 'revisions')
);
register_post_type('wp-ace-css',$args);
$labels = array(
'name' => _x( 'Admin Code Editor Pre JS', 'post type general name', 'admin-code-editor' ),
'singular_name' => _x( 'Admin Code Editor Pre JS', 'post type singular name', 'admin-code-editor' ),
'menu_name' => _x( 'Admin Code Editor Pre JS', 'admin menu', 'admin-code-editor' ),
'name_admin_bar' => _x( 'Admin Code Editor Pre JS', 'add new on admin bar', 'admin-code-editor' ),
'add_new' => _x( 'Add New', 'nw-item', 'admin-code-editor' ),
'add_new_item' => __( 'Add New Admin Code Editor Pre JS', 'admin-code-editor' ),
'new_item' => __( 'New Admin Code Editor Pre JS', 'admin-code-editor' ),
'edit_item' => __( 'Edit Admin Code Editor Pre JS', 'admin-code-editor' ),
'view_item' => __( 'View Admin Code Editor Pre JS', 'admin-code-editor' ),
'all_items' => __( 'All Admin Code Editor Pre JS', 'admin-code-editor' ),
'search_items' => __( 'Search Admin Code Editor Pre JS', 'admin-code-editor' ),
'parent_item_colon' => __( 'Parent Admin Code Editor Pre JS:', 'admin-code-editor' ),
'not_found' => __( 'No Admin Code Editor Pre JS found.', 'admin-code-editor' ),
'not_found_in_trash' => __( 'No Admin Code Editor Pre JS found in Trash.', 'admin-code-editor' )
);
$args = array(
'labels' => $labels,
'public' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'editor', 'author', 'revisions')
);
register_post_type('wp-ace-js',$args);
}
/**
* Explicitly set return value to array when empty
* @param type $hidden_templates
* @return array
* @since 1.0.0
*/
function filterDefaultHideonTemplates($hidden_templates) {
if (empty($hidden_templates)) {
$hidden_templates = array();
}
return $hidden_templates;
}
/**
* Explicitly set return value to array when empty
* @param type $code_editors
* @return array
* @since 1.0.0
*/
function filterDefaultHideCodeEditorTypes($code_editors) {
if (empty($code_editors)) {
$code_editors = array();
}
return $code_editors;
}
/**
* Explicitly set return value to array when empty
* @param type $conditional_display
* @return array
* @since 1.0.0
*/
function filterDefaultConditionalDisplay($conditional_display) {
if (empty($conditional_display)) {
$conditional_display = array();
}
return $conditional_display;
}
/**
* Explicitly set return value to array when empty
* @param type $conditional_display
* @return array
* @since 1.0.0
*/
function filterEnabledPostType($enabled_post_type) {
if (empty($enabled_post_type)) {
$enabled_post_type = array();
}
return $enabled_post_type;
}
/**
*
* Add options page for Admin Code Editor Settings
*
* @since 1.0.0
*/
function options_menu() {
add_options_page(
'Admin Code Editor Settings',
'Admin Code Editor',
'manage_options',
'admin-code-editor-options-page',
array(&$this,'admin_code_editor_settings_page')
);
}
/**
*
* Admin Code Editor Settings Page Callback
*
* @since 1.0.0
*/
function admin_code_editor_settings_page() {
?>
true
);
$post_types = get_post_types( $args, 'objects' );
$selected_post_types = get_option('wp_ace_enabled_post_type');
$is_checked_post_type = function($post_type_name) use ($selected_post_types) {
if (!empty($selected_post_types) && in_array($post_type_name, $selected_post_types) ) {
return ' checked ';
}
};
?>