base || WINP_SNIPPETS_POST_TYPE !== $current_screen->post_type ) {
return;
}
$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
;
' . esc_html( $dir ) . ''
);
?>
:
chmod
777
__( 'PHP Snippet', 'insert-php' ),
'css' => __( 'CSS Snippet', 'insert-php' ),
'js' => __( 'JS Snippet', 'insert-php' ),
'text' => __( 'Text Snippet', 'insert-php' ),
'html' => __( 'HTML Snippet', 'insert-php' ),
'advert' => __( 'Advertisement', 'insert-php' ),
'universal' => __( 'Universal', 'insert-php' ),
];
$this->render_snippet_dropdown( $strings, __( 'New Snippet: PHP', 'insert-php' ) );
}
/**
* Add quick buttons
*
* @return void
*/
public function current_screen_post() {
global $post;
$post_id = null;
if ( ! empty( $post ) && is_object( $post ) && isset( $post->ID ) ) {
$post_id = $post->ID;
} else {
$post_id = WINP_HTTP::get( 'post', null, 'absint' );
}
$type = WINP_Helper::get_snippet_type( $post_id );
$strings = [
'php' => __( 'PHP Snippet', 'insert-php' ),
'css' => __( 'CSS Snippet', 'insert-php' ),
'js' => __( 'JS Snippet', 'insert-php' ),
'text' => __( 'Text Snippet', 'insert-php' ),
'html' => __( 'HTML Snippet', 'insert-php' ),
'advert' => __( 'Advertisement', 'insert-php' ),
'universal' => __( 'Universal', 'insert-php' ),
];
$label_map = [
'php' => __( 'New Snippet: PHP', 'insert-php' ),
'css' => __( 'New Snippet: CSS', 'insert-php' ),
'js' => __( 'New Snippet: JS', 'insert-php' ),
'text' => __( 'New Snippet: Text', 'insert-php' ),
'html' => __( 'New Snippet: HTML', 'insert-php' ),
'advert' => __( 'New Snippet: Advertisement', 'insert-php' ),
'universal' => __( 'New Snippet: Universal', 'insert-php' ),
];
$button_label = isset( $label_map[ $type ] ) ? $label_map[ $type ] : __( 'New Snippet: PHP', 'insert-php' );
$this->render_snippet_dropdown( $strings, $button_label, ! empty( $post_id ) );
}
/**
* Render snippet type dropdown
*
* @param array $strings Snippet type labels (type => label).
* @param string $button_label Button label text.
* @param bool $show_title Whether to show the edit title.
*
* @return void
*/
private function render_snippet_dropdown( $strings, $button_label, $show_title = false ) {
$type_display_labels = [
'php' => __( 'PHP', 'insert-php' ),
'css' => __( 'CSS', 'insert-php' ),
'js' => __( 'JS', 'insert-php' ),
'text' => __( 'Text', 'insert-php' ),
'html' => __( 'HTML', 'insert-php' ),
'advert' => __( 'Advertisement', 'insert-php' ),
'universal' => __( 'Universal', 'insert-php' ),
];
// Get edit title if needed.
$edit_title = '';
if ( $show_title ) {
global $post;
$post_id = null;
if ( ! empty( $post ) && is_object( $post ) && isset( $post->ID ) ) {
$post_id = $post->ID;
} else {
$post_id = WINP_HTTP::get( 'post', null, 'absint' );
}
if ( ! empty( $post_id ) ) {
$type = WINP_Helper::get_snippet_type( $post_id );
$edit_labels = [
'php' => __( 'Edit PHP Snippet', 'insert-php' ),
'css' => __( 'Edit CSS Snippet', 'insert-php' ),
'js' => __( 'Edit JS Snippet', 'insert-php' ),
'text' => __( 'Edit Text Snippet', 'insert-php' ),
'html' => __( 'Edit HTML Snippet', 'insert-php' ),
'advert' => __( 'Edit Advertisement Snippet', 'insert-php' ),
'universal' => __( 'Edit Universal Snippet', 'insert-php' ),
];
$edit_title = isset( $edit_labels[ $type ] ) ? $edit_labels[ $type ] : '';
}
}
?>
post_type ) {
return;
}
if ( 'post' === $current_screen->base ) {
add_action( 'admin_head', [ $this, 'current_screen_post' ] );
}
if ( 'edit' === $current_screen->base ) {
add_action( 'admin_head', [ $this, 'current_screen_edit' ] );
}
}
/**
* Show the Permalink edit form
*
* @param string|\WP_Post $filename Filename or post object.
* @param string $permalink Permalink URL.
* @param string $filetype File type.
*
* @return void
*/
public function edit_form_before_permalink( $filename = '', $permalink = '', $filetype = 'css' ) {
$filetype = WINP_HTTP::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 int $post_id Post ID.
*
* @return void
*/
public function save_post( $post_id ) {
$nonce = WINP_HTTP::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_HTTP::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 = __( 'Woody Code Snippets', 'insert-php' );
$before = $after = '';
$snippet_linking = WINP_HTTP::post( 'wbcr_inp_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( '/' . 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;
}
}