Skip to content

Commit

Permalink
Use large image size in post content
Browse files Browse the repository at this point in the history
Closes #17

#17
  • Loading branch information
Konrad Karpieszuk committed Jan 9, 2022
1 parent 76f454c commit 7505708
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/EditorBox/Enqueues.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down
1 change: 1 addition & 0 deletions app/EditorBox/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}
Expand Down
16 changes: 15 additions & 1 deletion app/EditorBox/ProcessForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
Expand Down Expand Up @@ -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() ) ) ] );
Expand Down
5 changes: 2 additions & 3 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<img src="${resp.url}" />\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;
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7505708

Please sign in to comment.