post_type; } $enabled_post_types = array('post', 'page'); $this->options = get_option( 'wpacc_settings' ); if ( isset($this->options['selected_post_types']) ) { $enabled_post_types = array_merge( $enabled_post_types, $this->options['selected_post_types'] ); } return in_array( $post_type, $enabled_post_types ); } public function load_admin_scripts( $hook ) { if ( ( in_array( $hook, array('post.php', 'post-new.php') ) && $this->is_enabled_post_type() ) || $hook === 'toplevel_page_wp-add-custom-css_settings' ) { $this->options = get_option( 'wpacc_settings' ); if ( isset($this->options['enable_advanced_editor']) ) { wp_enqueue_style( 'wpacc_codemirror', plugin_dir_url( __FILE__ ) . 'lib/codemirror/codemirror.css' ); if ( isset($this->options['advanced_editor_theme']) && $this->options['advanced_editor_theme'] === 'dark' ) { wp_enqueue_style( 'wpacc_codemirror_dark', plugin_dir_url( __FILE__ ) . 'lib/codemirror/theme/tomorrow-night-bright.css', array('wpacc_codemirror') ); } wp_enqueue_script( 'wpacc_codemirror', plugin_dir_url( __FILE__ ) . 'lib/codemirror/codemirror.js' ); wp_enqueue_script( 'wpacc_codemirror_css', plugin_dir_url( __FILE__ ) . 'lib/codemirror/mode/css/css.js', array('wpacc_codemirror') ); wp_enqueue_script( 'wpacc_scripts', plugin_dir_url( __FILE__ ) . 'js/scripts.js', array('jquery', 'wpacc_codemirror_css') ); } } } public function add_meta_box( $post_type ) { if ( $this->is_enabled_post_type($post_type) ) { add_meta_box('wp_add_custom_css', __( 'Custom CSS', 'wp-add-custom-css' ), array( $this, 'render_meta_box_content' ), $post_type, 'advanced', 'high'); } } public function single_save( $post_id ) { if ( ! isset( $_POST['wp_add_custom_css_box_nonce'] ) || ! wp_verify_nonce( $_POST['wp_add_custom_css_box_nonce'], 'single_add_custom_css_box' ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( 'page' == $_POST['post_type'] ) { if ( ! current_user_can( 'edit_page', $post_id ) ) return; } else { if ( ! current_user_can( 'edit_post', $post_id ) ) return; } $single_custom_css = wp_kses( $_POST['single_custom_css'], array( '\'', '\"' ) ); update_post_meta( $post_id, '_single_add_custom_css', $single_custom_css ); } public function render_meta_box_content( $post ) { wp_nonce_field( 'single_add_custom_css_box', 'wp_add_custom_css_box_nonce' ); $single_custom_css = get_post_meta( $post->ID, '_single_add_custom_css', true ); $class = ( isset($this->options['advanced_editor_theme']) && $this->options['advanced_editor_theme'] === 'dark' ) ? ' class="wpacc_editor_dark"' : ''; echo '

'. sprintf( __( 'Add custom CSS rules for this %s', 'wp-add-custom-css' ), $post->post_type ). '

'; echo ''; } public function add_menu() { global $wpacc_settings_page; $wpacc_settings_page = add_menu_page( __('Wordpress Add Custom CSS', 'wp-add-custom-css'), __('Add Custom CSS', 'wp-add-custom-css'), 'manage_options', 'wp-add-custom-css_settings', array($this, 'create_settings_page'), plugin_dir_url( __FILE__ ) . 'images/icon.png'); } public function create_settings_page() { $this->options = get_option( 'wpacc_settings' ); ?>

options['main_custom_style'] ) ? esc_attr( $this->options['main_custom_style'] ) : ''; $class = ( isset($this->options['advanced_editor_theme']) && $this->options['advanced_editor_theme'] === 'dark' ) ? ' class="wpacc_editor_dark"' : ''; echo ''; } public function print_section_2_info() { echo __('Enable page specific CSS for the post types below.', 'wp-add-custom-css'); } public function post_types_checkboxes() { $available_post_types = get_post_types( array('public' => true, '_builtin' => false), 'objects' ); foreach ( $available_post_types as $post_type ) { if ( isset( $this->options['selected_post_types'] ) ) { $checked = in_array( $post_type->name, $this->options['selected_post_types'] ) ? ' checked' : ''; } else { $checked = ''; } echo '
' . $post_type->label . '
'; // output checkbox } } public function print_section_3_info() { echo __('Enable advanced css editor, including line numbers and code coloring.', 'wp-add-custom-css'); } public function advanced_editor_checkbox() { if ( isset( $this->options['enable_advanced_editor'] ) ) { $checked = ' checked'; } else { $checked = ''; } echo '
'; // output checkbox } public function advanced_editor_select() { echo '
'; } public function init_settings() { register_setting( 'wpacc_group', 'wpacc_settings' ); add_settings_section( 'wpacc_main_style', __('Main CSS', 'wp-add-custom-css'), array( $this, 'print_section_info' ), 'wp-add-custom-css_settings' ); add_settings_field( 'main_custom_style', __('CSS rules', 'wp-add-custom-css'), array( $this, 'main_css_input' ), 'wp-add-custom-css_settings', 'wpacc_main_style' ); add_settings_section( 'wpacc_post_types', __('Post types', 'wp-add-custom-css'), array( $this, 'print_section_2_info' ), 'wp-add-custom-css_settings' ); add_settings_field( 'selected_post_types', __('Available post types', 'wp-add-custom-css'), array( $this, 'post_types_checkboxes' ), 'wp-add-custom-css_settings', 'wpacc_post_types' ); add_settings_section( 'wpacc_advanced_editor', __('Advanced editor', 'wp-add-custom-css'), array( $this, 'print_section_3_info' ), 'wp-add-custom-css_settings' ); add_settings_field( 'enable_advanced_editor', __('Enable', 'wp-add-custom-css'), array( $this, 'advanced_editor_checkbox' ), 'wp-add-custom-css_settings', 'wpacc_advanced_editor' ); add_settings_field( 'advanced_editor_theme', __('Advanced editor layout', 'wp-add-custom-css'), array( $this, 'advanced_editor_select' ), 'wp-add-custom-css_settings', 'wpacc_advanced_editor' ); } public static function delete_options() { unregister_setting( 'wpacc_group', 'wpacc_settings' ); delete_option('wpacc_settings'); } public static function delete_custom_meta() { delete_post_meta_by_key('_single_add_custom_css'); } public static function add_wp_var($public_query_vars) { $public_query_vars[] = 'display_custom_css'; return $public_query_vars; } public static function display_custom_css(){ $display_css = get_query_var('display_custom_css'); if ($display_css == 'css'){ include_once (plugin_dir_path( __FILE__ ) . '/css/custom-css.php'); exit; } } public function add_custom_css() { $this->options = get_option( 'wpacc_settings' ); if ( isset($this->options['main_custom_style']) && $this->options['main_custom_style'] != '') { if ( function_exists('icl_object_id') ) { $css_base_url = site_url(); if ( is_ssl() ) { $css_base_url = site_url('/', 'https'); } } else { $css_base_url = get_bloginfo('url'); if ( is_ssl() ) { $css_base_url = str_replace('http://', 'https://', $css_base_url); } } wp_register_style( 'wp-add-custom-css', $css_base_url . '?display_custom_css=css' ); wp_enqueue_style( 'wp-add-custom-css' ); } } public function single_custom_css() { if ( is_single() || is_page() ) { if ( ! $this->is_enabled_post_type() ) { return; } global $post; $single_custom_css = get_post_meta( $post->ID, '_single_add_custom_css', true ); if ( $single_custom_css !== '' ) { $single_custom_css = str_replace ( '>' , '>' , $single_custom_css ); $output = "\n"; echo $output; } } } } } if(class_exists('Wpacc')) { add_action('template_redirect', array('Wpacc', 'display_custom_css')); register_uninstall_hook(__FILE__, array('Wpacc', 'uninstall')); $wpacc = new Wpacc(); } if(isset($wpacc)) { function wpacc_settings_link($links) { $settings_link = '' . __('Settings', 'wp-add-custom-css') . ''; array_unshift($links, $settings_link); return $links; } add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'wpacc_settings_link'); } ?>