* @copyright (c) 16.11.2018, Webcraftic * @version 1.0 */ // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { exit; } class WINP_Common_Snippet { /** * WINP_Common_Snippet constructor. */ public function __construct() { } /** * Register hooks */ public function registerHooks() { add_action( 'current_screen', [ $this, 'currentScreen' ] ); add_action( 'edit_form_before_permalink', [ $this, 'editFormBeforePermalink' ] ); add_action( 'admin_notices', [ $this, 'createUploadsDirectory' ] ); add_action( 'before_delete_post', [ $this, 'beforeDeletePost' ] ); add_action( 'save_post', [ $this, 'savePost' ] ); add_action( 'save_post_' . WINP_SNIPPETS_POST_TYPE, [ $this, 'save_snippet' ], 10, 3 ); add_action( 'auto-draft_to_publish', [ $this, 'publish_snippet' ] ); add_filter( 'script_loader_src', [ $this, 'unload_scripts' ], 10, 2 ); } /** * Create the custom-css-js dir in uploads directory * * Show a message if the directory is not writable * * Create an empty index.php file inside */ public function createUploadsDirectory() { $current_screen = get_current_screen(); // Check if we are editing a custom-css-js post if ( $current_screen->base != 'post' || $current_screen->post_type != WINP_SNIPPETS_POST_TYPE ) { return false; } $dir = WINP_UPLOAD_DIR; // Create the dir if it doesn't exist if ( ! file_exists( $dir ) ) { wp_mkdir_p( $dir ); } // Show a message if it couldn't create the dir if ( ! file_exists( $dir ) ) { ?>

winp-css-js' ); ?>

:
mkdir ;
chmod 777 ;

' . $dir . '' ); ?>

:
chmod 777

__( 'Php snippet', 'insert-php' ), 'text' => __( 'Text snippet', 'insert-php' ), 'css' => __( 'Css snippet', 'insert-php' ), 'js' => __( 'Js snippet', 'insert-php' ), 'html' => __( 'Html snippet', 'insert-php' ), 'universal' => __( 'Universal snippet', 'insert-php' ), ]; $url = 'post-new.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&winp_item='; ?> [ 'php' => __( 'Php snippet', 'insert-php' ), 'text' => __( 'Text snippet', 'insert-php' ), 'css' => __( 'Css snippet', 'insert-php' ), 'js' => __( 'Js snippet', 'insert-php' ), 'html' => __( 'Html snippet', 'insert-php' ), 'universal' => __( 'Universal snippet', 'insert-php' ), ], 'edit' => [ 'php' => __( 'Edit php snippet', 'insert-php' ), 'text' => __( 'Edit text snippet', 'insert-php' ), 'css' => __( 'Edit css snippet', 'insert-php' ), 'js' => __( 'Edit js snippet', 'insert-php' ), 'html' => __( 'Edit html snippet', 'insert-php' ), 'universal' => __( 'Edit universal snippet', 'insert-php' ), ] ]; $post_id = WINP_Plugin::app()->request->get( 'post', null ); if ( ! empty( $post_id ) ) { $action = 'edit'; } else { $action = 'add'; } $type = WINP_Helper::get_snippet_type( $post_id ); $html = $strings[ $action ][ $type ]; if ( 'edit' == $action ) { $url = 'post-new.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&winp_item='; $html .= ' '; $html .= '' . __( 'Add', 'insert-php' ) . ''; } ?> post_type ) { return; } // Код виджета поддержки пока что нужно скрыть // add_action( 'admin_footer', array( $this, 'admin_footer' ) ); if ( $current_screen->base == 'post' ) { add_action( 'admin_head', [ $this, 'currentScreenPost' ] ); } if ( $current_screen->base == 'edit' ) { add_action( 'admin_head', [ $this, 'currentScreenEdit' ] ); } } /** * Show the Permalink edit form * * @param string $filename * @param string $permalink * @param string $filetype */ public function editFormBeforePermalink( $filename = '', $permalink = '', $filetype = 'css' ) { $filetype = WINP_Plugin::app()->request->get( 'winp_item', $filetype, true ); if ( ! in_array( $filetype, [ 'css', 'js' ] ) ) { return; } if ( ! is_string( $filename ) ) { global $post; if ( ! is_object( $post ) ) { return; } if ( WINP_SNIPPETS_POST_TYPE !== $post->post_type ) { return; } $post = $filename; $slug = WINP_Helper::getMetaOption( $post->ID, 'css_js_slug', '' ); $default_filetype = WINP_Helper::getMetaOption( $post->ID, 'filetype', '' ); if ( $default_filetype ) { $filetype = $default_filetype; } else { $filetype = WINP_Helper::get_snippet_type( $post->ID ); } if ( ! in_array( $filetype, [ 'css', 'js' ] ) ) { return; } if ( ! @file_exists( WINP_UPLOAD_DIR . '/' . $slug . '.' . $filetype ) ) { $slug = false; } $filename = ( $slug ) ? $slug : $post->ID; } if ( empty( $permalink ) ) { $permalink = WINP_UPLOAD_URL . '/' . $filename . '.' . $filetype; } ?>
: .
post_type ) { return; } if ( ! wp_is_writable( WINP_UPLOAD_DIR ) ) { return; } $default_filetype = WINP_Helper::get_snippet_type( $post->ID ); $filetype = WINP_Helper::getMetaOption( $postid, 'filetype', $default_filetype ); if ( ! in_array( $filetype, [ 'css', 'js' ] ) ) { return; } $slug = WINP_Helper::getMetaOption( $postid, 'css_js_slug' ); $file_name = $postid . '.' . $filetype; @unlink( WINP_UPLOAD_DIR . '/' . $file_name ); if ( ! empty( $slug ) ) { @unlink( WINP_UPLOAD_DIR . '/' . $slug . '.' . $filetype ); } } /** * Save post * * @param $post_id */ public function savePost( $post_id ) { $nonce = WINP_Plugin::app()->request->post( 'winp-permalink-nonce' ); if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'winp-permalink' ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( WINP_SNIPPETS_POST_TYPE != WINP_Plugin::app()->request->post( 'post_type' ) ) { return; } $default_filetype = WINP_Helper::get_snippet_type( $post_id ); $filetype = WINP_Helper::getMetaOption( $post_id, 'filetype', $default_filetype ); if ( ! in_array( $filetype, [ 'css', 'js' ] ) ) { return; } $plugin_title = WINP_Plugin::app()->getPluginTitle(); $before = $after = ''; $snippet_linking = WINP_Plugin::app()->request->post( WINP_Plugin::app()->getPrefix() . 'snippet_linking' ); $linking = ! empty( $snippet_linking ) ? $snippet_linking : 'external'; $content = get_post( $post_id )->post_content; // Save the Custom Code in a file in `wp-content/uploads/winp-css-js` if ( 'inline' == $linking ) { $before = '' . PHP_EOL; $after = '' . PHP_EOL; if ( 'css' == $filetype ) { $before .= '' . PHP_EOL . $after; } if ( 'js' == $filetype ) { if ( ! preg_match( '/]*>([\s\S]*?)<\/script>/im', $content ) ) { $before .= '' . PHP_EOL . $after; } else { // the content has a post_type == WINP_SNIPPETS_POST_TYPE ) { // Unload ckeditor.js from theme The Rex if ( 'bk-ckeditor-js' == $handle ) { return false; } } return $src; } }