From 75057089ed449ea0d61c71aee0d78c8d6f3744ae Mon Sep 17 00:00:00 2001 From: Konrad Karpieszuk Date: Sun, 9 Jan 2022 18:40:32 +0100 Subject: [PATCH] Use large image size in post content Closes #17 https://github.com/kkarpieszuk/Editor-Box/issues/17 --- app/EditorBox/Enqueues.php | 4 ++-- app/EditorBox/Hooks.php | 1 + app/EditorBox/ProcessForms.php | 16 +++++++++++++++- js/editor.js | 5 ++--- readme.txt | 1 + 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/EditorBox/Enqueues.php b/app/EditorBox/Enqueues.php index 491b9b6..869467f 100644 --- a/app/EditorBox/Enqueues.php +++ b/app/EditorBox/Enqueues.php @@ -4,8 +4,8 @@ class Enqueues { public function enqueue_editor_stuff() { - wp_enqueue_style( 'editor_box_style', plugins_url( 'css/editor.css', EDITORBOX_PLUGIN_FILE ), [], '1.1.1641734139' ); // date '+%s' - wp_enqueue_script( 'editor_box_script', plugins_url( 'js/editor.js', EDITORBOX_PLUGIN_FILE ), [], '1.1.1641734139' ); + wp_enqueue_style( 'editor_box_style', plugins_url( 'css/editor.css', EDITORBOX_PLUGIN_FILE ), [], '1.1.1641749900' ); // date '+%s' + wp_enqueue_script( 'editor_box_script', plugins_url( 'js/editor.js', EDITORBOX_PLUGIN_FILE ), [], '1.1.1641749900' ); wp_localize_script('editor_box_script', 'editor_box_int', [ 'ajaxurl' => admin_url('admin-ajax.php'), 'publish_button_value' => __( 'Publish', 'editor_box' ), diff --git a/app/EditorBox/Hooks.php b/app/EditorBox/Hooks.php index 3c5de53..82b98ec 100644 --- a/app/EditorBox/Hooks.php +++ b/app/EditorBox/Hooks.php @@ -8,6 +8,7 @@ public function register_hooks() { add_action( 'init', [ new ProcessForms(), 'process_post' ], 10, 0 ); add_action( 'init', [ new Internationalization(), 'load_text_domain' ] ); add_action( 'wp_enqueue_scripts', [ new Enqueues, 'enqueue_editor_stuff' ] ); + add_filter( 'wp_kses_allowed_html', [ new ProcessForms(), 'kses_allowed_html_filter' ], 10, 2 ); add_action( 'wp_ajax_nopriv_editor_box_file', [ new ProcessForms(), 'save_editor_box_file' ] ); add_action( 'wp_ajax_editor_box_file', [ new ProcessForms(), 'save_editor_box_file' ] ); } diff --git a/app/EditorBox/ProcessForms.php b/app/EditorBox/ProcessForms.php index a6f9bff..ee4f8c7 100644 --- a/app/EditorBox/ProcessForms.php +++ b/app/EditorBox/ProcessForms.php @@ -3,6 +3,18 @@ namespace EditorBox; class ProcessForms { + + public function kses_allowed_html_filter( $tags, $context ) { + $tags['img']['sizes'] = true; + $tags['img']['srcset'] = true; + $tags['source'] = array( + 'srcset' => true, + 'sizes' => true, + 'type' => true, + ); + return $tags; + } + public function process_post() { if ( $this->publish_button_clicked() && current_user_can( "edit_posts" ) @@ -78,8 +90,10 @@ public function save_editor_box_file() { // Add the metadata. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); + + $imghtml = wp_get_attachment_image( $id, 'large' ); - wp_send_json( [ 'url' => $url ] ); + wp_send_json( [ 'imghtml' => $imghtml ] ); } } else { wp_send_json( [ 'error' => sprintf( __( 'Incorrect file type or file bigger than %s.', 'editor_box' ), size_format( wp_max_upload_size() ) ) ] ); diff --git a/js/editor.js b/js/editor.js index f9614f8..b014202 100644 --- a/js/editor.js +++ b/js/editor.js @@ -40,10 +40,9 @@ document.addEventListener('DOMContentLoaded', function () { request.onload = function() { if ( this.status >= 200 && this.status < 400 ) { var resp = JSON.parse( this.response ); - if ( resp.url !== undefined ) { // if url has been returned - let imageElement = `\n\n`; + if ( resp.imghtml !== undefined ) { // if url has been returned const textarea = document.getElementById('editor_box_content'); - textarea.value = ( textarea.value + imageElement ); + textarea.value = ( textarea.value + resp.imghtml ); } else if ( resp.error !== undefined ) { // if error has been returned ajax_errors.style.display = 'block'; ajax_errors.innerText = resp.error; diff --git a/readme.txt b/readme.txt index 54617ec..b5bb9b3 100644 --- a/readme.txt +++ b/readme.txt @@ -61,4 +61,5 @@ Like on Facebook or Twitter, post title is not obligatory to provide. If you sav * Feature - Added visible notification about image being uploaded * Feature - Post can be saved as draft instead immediate publishing (press Ctrl button over Publish button to switch mode) * Enhancement - Added assets versioning to help refreshing cached versions between plugin releases +* Enhancement - Use large image size in rendered html instead of full size * Fix - Fixed issue with not handled image uploads when image size bigger than PHP upload_max_filesize \ No newline at end of file