Changeset 2184011
- Timestamp:
- 10/31/2019 04:51:54 PM (6 years ago)
- Location:
- simple-multiple-featured-images/trunk
- Files:
-
- 4 edited
-
admin/js/jquery.smfi.js (modified) (39 diffs)
-
languages/simple-multiple-featured-images-de_DE.mo (modified) (previous)
-
languages/simple-multiple-featured-images-de_DE.po (modified) (4 diffs)
-
simple-multiple-featured-images.php (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
simple-multiple-featured-images/trunk/admin/js/jquery.smfi.js
r1962166 r2184011 4 4 * @author Roman Bauer. 5 5 * @since 1.0.0 6 * @since 1.1.0 Added image limit check. 6 7 */ 7 8 jQuery('document').ready( function( jQuery ){ 8 9 9 10 // Search smfi metabox, 10 11 var metaBox = jQuery( '#smfi-metabox.postbox' ); 11 12 12 13 if( metaBox.length ) { 13 14 15 14 16 // Get error container which will be used for error reporting. 15 17 var errorContainer = metaBox.find( '#smfi-error-container' ); 16 18 17 19 // Get add image button. 18 20 var addNewImgBtn = metaBox.find( '#smfi-add-new-img-btn' ); 19 21 20 22 // Get image container. 21 23 var imgContainer = metaBox.find( '.smfi-img-container' ); 22 24 25 // Get image limit. 26 var imgLimitContainer = metaBox.find( '#smfi-img-limit' ); 27 var imgLimit = parseInt( imgLimitContainer.attr( 'value' ), 10 ); 28 29 // Get post id. 30 var postID = parseInt( metaBox.find( '#smfi-post-id' ).attr( 'value' ), 10 ); 31 23 32 /* 24 33 * Open media frame as soon as the add new image button was clicked. 25 34 * This allows the user to add a new image from the media library. 26 35 */ 27 de.smfi.MediaFrameManager.openMediaFrameOnAddBtnClick( addNewImgBtn, imgContainer, errorContainer );36 de.smfi.MediaFrameManager.openMediaFrameOnAddBtnClick( postID, addNewImgBtn, imgContainer, imgLimit, errorContainer ); 28 37 29 38 // Enable removal and modification of existing images. 30 39 imgContainer.find( '.smfi-img' ).each( function( index, element ) { 31 40 32 41 /* 33 42 * Open media frame as soon as an existing image was clicked. … … 35 44 */ 36 45 de.smfi.MediaFrameManager.openMediaFrameOnImgClick( jQuery( element ), errorContainer ); 37 46 38 47 // Remove image as soon as its remove button was clicked. 39 48 de.smfi.MediaFrameManager.removeImgOnBtnClick( jQuery( element ).siblings( '.smfi-remove-img-btn' ) ); … … 44 53 var de = de || { 45 54 smfi : { 46 55 47 56 ImageAddedListener : [], 48 57 49 58 ImageRemovedListener : [], 50 59 51 60 /** 52 61 * Contains translated textes for the UI. If no translation available a default one is used. … … 58 67 */ 59 68 UIText : { 60 69 61 70 /** 62 71 * The title for the media frame. … … 68 77 */ 69 78 mediaFrameTitle: typeof smfi_translation_object !== 'undefined' ? smfi_translation_object.media_frame_title : 'Select image', 70 79 71 80 /** 72 81 * The text for the media frame add button. … … 79 88 mediaFrameAddBtnTxt: typeof smfi_translation_object !== 'undefined' ? smfi_translation_object.media_frame_add_btn_txt : 'Add image', 80 89 }, 81 90 82 91 /** 83 92 * Helper in context of working with images. … … 89 98 */ 90 99 ImageHelper : { 91 100 92 101 /** 93 102 * Compared the ID of the given image with the given id. … … 104 113 var imgWrapper = img.parent(); 105 114 var hiddenInputWithImgID = imgWrapper.find( '[name="smfi-img-ids[]"]' ); 106 115 107 116 var parsedImgId = parseInt( hiddenInputWithImgID.attr( 'value'), 10 ); 108 117 var parsedIdToCompare = parseInt( idToCompare, 10 ); 109 118 110 119 if( Number.isNaN( parsedImgId ) || Number.isNaN( parsedIdToCompare ) ) { 111 120 return false; … … 114 123 } 115 124 }, 116 125 117 126 /** 118 127 * Handles the error handling. … … 128 137 * Returns the default error message. 129 138 * 130 * It uses the received translated default error message from the server. If no error message was transmitted then a default one is used 131 * which is specified inside this function. 139 * It uses the received translated default error message from the server. If no error message was transmitted then a default one is used. 132 140 * 133 141 * @since 1.0.0 … … 137 145 */ 138 146 getDefaultErrorMessageTxt : function() { 139 if( typeof smfi_translation_object.default_error_message !== 'undefined') {147 if( typeof smfi_translation_object.default_error_message !== 'undefined' ) { 140 148 return smfi_translation_object.default_error_message; 141 149 } else { … … 143 151 } 144 152 }, 145 153 146 154 /** 147 155 * Returns the error message for failed adding an image. 148 156 * 149 * It uses the received translated error message from the server. If no error message was transmitted then a default one is used 150 * which is specified inside this function. 157 * It uses the received translated error message from the server. If no error message was transmitted then a default one is used. 151 158 * 152 159 * @since 1.0.0 … … 156 163 */ 157 164 getAddImageErrorMessage : function() { 158 if( typeof smfi_translation_object.add_image_error_message !== 'undefined') {165 if( typeof smfi_translation_object.add_image_error_message !== 'undefined' ) { 159 166 return smfi_translation_object.add_image_error_message; 160 167 } else { … … 162 169 } 163 170 }, 164 171 165 172 /** 166 173 * Returns the error message for failed changing an image. 167 174 * 168 * It uses the received translated error message from the server. If no error message was transmitted then a default one is used 169 * which is specified inside this function. 175 * It uses the received translated error message from the server. If no error message was transmitted then a default one is used. 170 176 * 171 177 * @since 1.0.0 … … 175 181 */ 176 182 getChangeImageErrorMessage : function() { 177 if( typeof smfi_translation_object.change_image_error_message !== 'undefined') {183 if( typeof smfi_translation_object.change_image_error_message !== 'undefined' ) { 178 184 return smfi_translation_object.change_image_error_message; 179 185 } else { … … 181 187 } 182 188 }, 183 189 190 /** 191 * Returns the error message for an exceeded image limit. 192 * 193 * It uses the received translated error message from the server. If no error message was transmitted then a default one is used. 194 * 195 * @since 1.1.0 196 * @access private 197 * 198 * @return {string} Error message. 199 */ 200 getImageLimitExceededErrorMessage : function() { 201 if( typeof smfi_translation_object.image_limit_exceeded_error_message !== 'undefined' ) { 202 return smfi_translation_object.image_limit_exceeded_error_message; 203 } else { 204 return getDefaultErrorMessageTxt(); 205 } 206 }, 207 184 208 /** 185 209 * Shows the given error message inside the given error container. … … 197 221 */ 198 222 showError : function( errorContainer, errorMessageTxt ) { 199 223 200 224 // Create new error. 201 225 var newError = jQuery( document.createElement( 'div' ) ); 202 226 newError.attr( 'class', 'notice notice-error is-dismissible' ); 203 227 errorContainer.append( newError ); 204 228 205 229 // Create error message and append it to the error. 206 230 var errorMessage = jQuery( document.createElement( 'p' ) ); … … 210 234 errorMessage.text( this.getDefaultErrorMessageTxt() ); 211 235 } 212 236 213 237 newError.append( errorMessage ); 214 238 215 239 // Create close button which allows to remove the error message. 216 240 var closeBtn = jQuery( document.createElement( 'button' ) ); 217 241 closeBtn.attr( 'class', 'notice-dismiss' ); 218 242 newError.append( closeBtn ); 219 243 220 244 // Remove error message if close button was clicked. 221 245 closeBtn.click( function( event ) { … … 227 251 } ); 228 252 } ); 229 253 230 254 // Show error in the error container. 231 255 errorContainer.append( newError ); 232 256 } 233 257 }, 234 258 235 259 /** 236 260 * Handles interaction with the media frame. … … 242 266 */ 243 267 MediaFrameManager : { 244 268 245 269 /** 246 270 * Creates and returns a new media frame which allows to select images. … … 252 276 */ 253 277 getNewMediaFrame : function() { 254 278 255 279 // Accepts an optional object hash to override default values. 256 280 var mediaFrame = new wp.media.view.MediaFrame.Select( { 257 281 258 282 // Modal title. 259 283 title: de.smfi.UIText.mediaFrameTitle, … … 288 312 return mediaFrame; 289 313 }, 290 314 291 315 /** 292 316 * Adds a new image by the specified image ID. 293 317 * 294 318 * @since 1.0.0 295 * @access private 296 * 297 * @param {number} newImgID The image ID of the new image. 298 * @param {jQuery} imgContainer The image container which contains all images. 299 * @param {jQuery} errorContainer The error container which can be used for reporting ocurred errors to the user. 300 */ 301 addNewImg : function( newImgID, imgContainer, errorContainer) { 302 319 * @since 1.1.0 Transmit also data for the image limit check on the server. 320 * @access private 321 * 322 * @param {number} newImgID The image ID of the new image. 323 * @param {jQuery} imgContainer The image container which contains all images. 324 * @param {number} postID The ID of the current showed post. 325 * @param {jQuery} errorContainer The error container which can be used for reporting ocurred errors to the user. 326 */ 327 addNewImg : function( newImgID, imgContainer, postID, errorContainer) { 328 303 329 // Setup data for ajax request. 304 330 var requestData = { 305 331 306 332 // Specify which ajax callback should be triggered on server side. 307 333 'action': 'get_img_wrapper_html_by_ajax_as_json', 308 334 309 335 // Transmit the received nonce back to server. 310 336 'smfi_add_img_security': ajax_smfi_object.ajax_add_image_nonce, 311 337 338 /* 339 * Transmit the received post id of the current post back to server. 340 * It will be used on server side to check if it is possible to add more images to the current post. 341 */ 342 'smfi_post_id': postID, 343 312 344 // ID of new image. 313 'smfi_img_id': newImgID 345 'smfi_img_id': newImgID, 346 347 // Number of current images. 348 'smfi_number_of_images': imgContainer.children( '.smfi-img-wrapper' ).length 314 349 }; 315 350 316 351 // Make an ajax request and get the html of the new image. 317 352 mediaFrameManager = this; … … 321 356 type:'POST', 322 357 data: requestData, 323 358 324 359 success: function( response ){ 325 360 326 361 if( response.hasOwnProperty( 'newImgHtml' ) ) { 327 362 328 363 // Add new image wrapper into the DOM 329 364 var newImgHtml = jQuery( response.newImgHtml ).prependTo( imgContainer ); 330 365 331 366 // Invoke all registered listener and let them do their stuff with the new image. 332 367 for( var i = 0; i < de.smfi.ImageAddedListener.length; i++ ) { … … 335 370 }; 336 371 } 337 372 338 373 /* 339 374 * Open media frame as soon as the new image was clicked. … … 342 377 var newImg = newImgHtml.find( '.smfi-img' ); 343 378 mediaFrameManager.openMediaFrameOnImgClick( newImg, errorContainer ); 344 379 345 380 // Remove the new image as soon as its remove button was clicked. 346 381 mediaFrameManager.removeImgOnBtnClick( newImg.siblings( '.smfi-remove-img-btn' ) ); 347 382 } else { 348 383 349 384 var hasErrorMessage = response.hasOwnProperty( 'data' ) && response.data.hasOwnProperty( 'smfiErrorMessage' ); 350 385 if( hasErrorMessage ) { … … 363 398 }); 364 399 }, 365 400 366 401 /** 367 402 * Updates an existing image with the new selected image by the user. 368 403 * 369 * The user select the image via media frame. For this an ajax request is done. 404 * The user select the image via media frame. For this an ajax request is done. 370 405 * If the user selects the same image then no update will be executed. 371 406 * … … 378 413 */ 379 414 updateImg : function( newImgID, oldImg, errorContainer) { 380 415 381 416 // Setup data for ajax request. 382 417 var requestData = { 383 418 384 419 // Specify which ajax callback should be triggered on server side. 385 420 'action': 'get_img_html_by_ajax_as_json', 386 421 387 422 // Transmit the received nonce back to server. 388 423 'smfi_change_img_security': ajax_smfi_object.ajax_change_image_nonce, 389 424 390 425 // ID of new image. 391 426 'smfi_img_id': newImgID 392 427 }; 393 428 394 429 // Make an ajax request and get the html of the new image. 395 430 mediaFrameManager = this; … … 399 434 type:'POST', 400 435 data: requestData, 401 436 402 437 success: function(response){ 403 438 404 439 if( response.hasOwnProperty( 'newImgHtml' ) ) { 405 440 406 441 // Update the image ID in the hidden input. 407 442 var imgWrapper = oldImg.parent(); 408 443 var hiddenInputWithImgID = imgWrapper.find( '[name="smfi-img-ids[]"]' ); 409 444 hiddenInputWithImgID.attr( 'value', newImgID ); 410 445 411 446 // Update image html. 412 447 oldImg.replaceWith(response.newImgHtml); … … 418 453 var img = imgWrapper.find( '.smfi-img' ); 419 454 mediaFrameManager.openMediaFrameOnImgClick( img, errorContainer ); 420 455 421 456 } else { 422 457 423 458 var hasErrorMessage = response.hasOwnProperty( 'data' ) && response.data.hasOwnProperty( 'smfiErrorMessage' ); 424 459 if( hasErrorMessage ) { … … 437 472 }); 438 473 }, 439 474 440 475 /** 441 476 * Adds a listener which opens the media frame on clicking the add new image button. … … 444 479 * 445 480 * @since 1.0.0 481 * @since 1.1.0 Added image limit check. 446 482 * @access private 447 483 * … … 449 485 * @listens select 450 486 * 487 * @param {number} postID The ID of the current showed post. 451 488 * @param {jQuery} btn The button. 452 489 * @param {jQuery} imgContainer The image container. 490 * @param {number} imgLimit The image limit. 453 491 * @param {jQuery} errorContainer The error container which should contain possible the error messages. 454 492 */ 455 openMediaFrameOnAddBtnClick : function( btn, imgContainer, errorContainer ) {456 493 openMediaFrameOnAddBtnClick : function( postID, btn, imgContainer, imgLimit, errorContainer ) { 494 457 495 // Create new media frame 458 496 var mediaFrame = this.getNewMediaFrame(); 459 497 460 498 // Open the media frame if button was clicked. 461 499 btn.click( function( event ) { 462 500 event.preventDefault(); 463 mediaFrame.open(); 464 } ); 465 501 /* 502 * Check if the given image limit is valid. 503 * It is valid if it is an integer and greater than -1. -1 means that unlimited number of pictures can be added. 504 */ 505 if( Number.isInteger( imgLimit ) && imgLimit >= -1 ) { 506 let numberOfImages = imgContainer.children( '.smfi-img-wrapper' ).length; 507 /* 508 * Allow adding another image if the image limit is set to -1 or the current number of images is below the image limit. 509 * A limit of -1 means that unlimited number of pictures can be added. 510 */ 511 if( imgLimit === -1 || numberOfImages >= 0 && numberOfImages < imgLimit ) { 512 mediaFrame.open(); 513 } else { 514 // Image limit is reached -> Show corresponding error message to the user. 515 de.smfi.ErrorHandler.showError( errorContainer, de.smfi.ErrorHandler.getImageLimitExceededErrorMessage() ); 516 } 517 } else { 518 // Adding image failed because the image limit is invalid for some reason -> Show the default add image error. 519 de.smfi.ErrorHandler.showError( errorContainer, de.smfi.ErrorHandler.getAddImageErrorMessage() ); 520 } 521 } ); 522 466 523 // Add new image if something was selected by the user. 467 524 mediaFrameManager = this; … … 470 527 // Get selected image 471 528 var selectedAttachment = mediaFrame.state().get( 'selection' ).first().toJSON(); 472 529 473 530 if( typeof selectedAttachment.id !== 'undefined' ) { 474 531 // Create new image wrapper which shows the new image. 475 mediaFrameManager.addNewImg( selectedAttachment.id, imgContainer, errorContainer );532 mediaFrameManager.addNewImg( selectedAttachment.id, imgContainer, postID, errorContainer ); 476 533 } else { 477 // Show error with default error message.534 // Show add image error. 478 535 de.smfi.ErrorHandler.showError( errorContainer, de.smfi.ErrorHandler.getAddImageErrorMessage() ); 479 536 } 480 537 } ); 481 482 }, 483 538 539 }, 540 484 541 /** 485 542 * Adds a listener which opens the media frame on clicking the given image. … … 500 557 */ 501 558 openMediaFrameOnImgClick : function( img, errorContainer ) { 502 559 503 560 // Create new media frame 504 561 var mediaFrame = this.getNewMediaFrame(); 505 562 506 563 // Open the media frame if image was clicked. 507 564 img.click( function( event ) { … … 509 566 mediaFrame.open(); 510 567 } ); 511 568 512 569 // Update the image if user select a new image via media frame. 513 570 mediaFrameManager = this; 514 571 mediaFrame.on( 'select', function() { 515 572 516 573 // Get id of the new selected image 517 574 var selectedAttachment = mediaFrame.state().get( 'selection' ).first().toJSON(); 518 575 var newImgID = selectedAttachment.id; 519 576 520 577 if(typeof newImgID !== 'undefined') { 521 578 522 579 // If the selected image is the same as the current image then no update is necessary. 523 580 if(de.smfi.ImageHelper.hasImageId(img, newImgID)) { 524 581 return; 525 582 } 526 583 527 584 // Update existing image by the new selected image. 528 585 mediaFrameManager.updateImg( newImgID, img, errorContainer ); … … 532 589 } 533 590 } ); 534 591 535 592 // Preselect the clicked image inside the opened media frame. 536 593 mediaFrame.on( 'open',function() { 537 594 538 595 // Get image ID from hidden input 539 596 var imgWrapper = img.parent(); 540 597 var hiddenInputWithImgID = imgWrapper.find( '[name="smfi-img-ids[]"]' ); 541 598 var imgID = parseInt( hiddenInputWithImgID.attr( 'value' ) , 10); 542 599 543 600 if( Number.isInteger( imgID ) && imgID > 0 ) { 544 545 /* 601 602 /* 546 603 * Get/Create media frame attachment by using the image ID. 547 604 * The attachment can be used to set the selection inside the opened media frame properly. … … 549 606 attachment = wp.media.attachment(imgID); 550 607 attachment.fetch(); 551 608 552 609 // Get current media frame selection and add the image attachment to it. 553 610 if( attachment !== 'undefined') { … … 557 614 } 558 615 } 559 616 560 617 /* 561 618 * If preselection failed then just log the error. 562 619 * I think it does not necessary to show this error to the user because it is not critical enough. 563 620 * The user can continue working without a preselection. 564 */ 621 */ 565 622 console.log('SMFI Plugin: Could not preselect image in media frame.'); 566 623 } ); 567 624 }, 568 625 569 626 /** 570 627 * Adds a click listener to the given button which removes the image it belongs to. … … 579 636 removeImgOnBtnClick : function( removeBtn ) { 580 637 removeBtn.click( function( event ) { 581 638 582 639 event.preventDefault(); 583 640 584 641 // Remove the entire image container which is the parent of the remove button. 585 642 var removedImageWrapper = jQuery( this ).parent().remove(); 586 643 587 644 // Invoke all registered listener and let them do their stuff in context of the removed image. 588 645 for( var i = 0; i < de.smfi.ImageRemovedListener.length; i++ ) { -
simple-multiple-featured-images/trunk/languages/simple-multiple-featured-images-de_DE.po
r1971107 r2184011 6 6 "Project-Id-Version: SMFI Pot v1.0.0\n" 7 7 "Report-Msgid-Bugs-To: Translator Name <translations@example.com>\n" 8 "POT-Creation-Date: 201 8-11-08 18:33+0100\n"8 "POT-Creation-Date: 2019-10-31 15:45+0100\n" 9 9 "PO-Revision-Date: \n" 10 10 "Last-Translator: \n" … … 21 21 "_x:1,2c;_n:1,2;_n_noop:1,2;__ngettext:1,2;__ngettext_noop:1,2;_c,_nc:4c,1,2\n" 22 22 "X-Poedit-Basepath: ..\n" 23 "X-Generator: Poedit 2. 1.1\n"23 "X-Generator: Poedit 2.2.4\n" 24 24 "X-Poedit-SearchPath-0: simple-multiple-featured-images.php\n" 25 25 "X-Poedit-SearchPath-1: includes/class-smfi-dnd.php\n" 26 26 "X-Poedit-SearchPath-2: includes/class-smfi-shortcodes.php\n" 27 27 28 #: includes/class-smfi-dnd.php:1 8628 #: includes/class-smfi-dnd.php:191 29 29 msgid "The featured images could not be saved in the correct order." 30 30 msgstr "" … … 39 39 msgstr "SMFI Slider einfügen" 40 40 41 #: simple-multiple-featured-images.php: 26841 #: simple-multiple-featured-images.php:304 42 42 msgid "Select image" 43 43 msgstr "Bild auswählen" 44 44 45 #: simple-multiple-featured-images.php: 26946 #: simple-multiple-featured-images.php: 89145 #: simple-multiple-featured-images.php:305 46 #: simple-multiple-featured-images.php:1023 47 47 msgid "Add image" 48 48 msgstr "Bild hinzufügen" 49 49 50 #: simple-multiple-featured-images.php: 39850 #: simple-multiple-featured-images.php:450 51 51 msgid "More Featured Images" 52 52 msgstr "Weitere Beitragsbilder" 53 53 54 #: simple-multiple-featured-images.php: 64254 #: simple-multiple-featured-images.php:711 55 55 msgid "Saving the featured images failed." 56 56 msgstr "Das Speichern der Beitragsbilder ist fehlgeschlagen." 57 57 58 #: simple-multiple-featured-images.php: 79758 #: simple-multiple-featured-images.php:866 59 59 msgid "An error occured." 60 60 msgstr "Ein Fehler ist aufgetreten." 61 61 62 #: simple-multiple-featured-images.php:8 1162 #: simple-multiple-featured-images.php:880 63 63 msgid "Adding the image failed." 64 64 msgstr "Das Hinzufügen des Bildes ist fehlgeschlagen." 65 65 66 #: simple-multiple-featured-images.php:8 2866 #: simple-multiple-featured-images.php:896 67 67 msgid "Changing the image failed." 68 68 msgstr "Das Ändern des Bildes ist fehlgeschlagen." 69 69 70 #: simple-multiple-featured-images.php:845 70 #: simple-multiple-featured-images.php:912 71 msgid "" 72 "Adding a picture failed because the limit of the maximum possible pictures " 73 "is reached. To add more pictures, the limit must be raised. " 74 msgstr "" 75 "Das Hinzufügen des Bildes ist fehlgeschlagen, weil die maximale Anzahl an " 76 "Bildern erreicht wurde. Um weitere Bilder hinzuzufügen, muss das Limit " 77 "angehoben werden. " 78 79 #: simple-multiple-featured-images.php:928 80 msgid "Adding picture failed because of an invalid picture id. " 81 msgstr "" 82 "Das Hinzufügen des Bildes ist wegen einer ungültigen Bild-ID fehlgeschlagen. " 83 84 #: simple-multiple-featured-images.php:944 85 msgid "Adding picture failed because of an invalid post id. " 86 msgstr "" 87 "Das Hinzufügen des Bildes ist wegen einer ungültigen Post-ID fehlgeschlagen. " 88 89 #: simple-multiple-featured-images.php:960 90 msgid "Adding picture failed because of a security issue. " 91 msgstr "Das Hinzufügen des Bildes ist aus Sicherheitsgründen fehlgeschlagen. " 92 93 #: simple-multiple-featured-images.php:977 71 94 msgid "" 72 95 "Please contact the responsible person for your website or the plugin " … … 76 99 "Entwickler." 77 100 78 #: simple-multiple-featured-images.php:1 015101 #: simple-multiple-featured-images.php:1148 79 102 msgid "Remove image" 80 103 msgstr "Bild entfernen" -
simple-multiple-featured-images/trunk/simple-multiple-featured-images.php
r2041599 r2184011 4 4 Plugin URI: https://roman-bauer-web.de/wordpress-plugin-smfi 5 5 Description: Allows to add multiple featured images. 6 Version: 1. 0.66 Version: 1.1.0 7 7 Author: Roman Bauer 8 8 Author URI: https://roman-bauer-web.de/ … … 308 308 'add_image_error_message' => $this -> get_add_image_failed_error_message(), 309 309 'change_image_error_message' => $this -> get_change_image_failed_error_message(), 310 'image_limit_exceeded_error_message' => $this -> get_image_limit_exceeded_error_message_on_adding_image(), 310 311 ); 311 312 … … 381 382 * 382 383 * @since 1.0.0 384 * @since 1.1.0 Added image limit check and updated exception messages. 383 385 * 384 386 * @return json JSON with 'newImgHtml' property which contains the html or 'smfiErrorMessage' property which contains the error message … … 393 395 if( ! check_ajax_referer( $this -> nonce_manager::NONCE_ACTION_IMG_ADD, $this -> nonce_manager::NONCE_NAME_IMG_ADD, $die_if_invalid ) ) { 394 396 // Get an invalid nonce. 395 throw new Exception( "Received invalid nonce during add new image request via ajax" ); 396 } 397 398 // Get requested image by received id and response the img html to the client. 397 throw new Exception( $this -> get_invalid_nonce_error_message_on_adding_image() ); 398 } 399 400 // Check if the received post id belongs to a existing post. 401 $post_id = isset( $_POST['smfi_post_id'] ) && is_numeric( $_POST['smfi_post_id'] ) ? intval( $_POST['smfi_post_id'], 10 ) : -1; 402 if( get_post_status( $post_id ) === false ) { 403 // Received invalid post id for some reason. 404 throw new Exception( $this -> get_invalid_post_id_error_message_on_adding_image() ); 405 } 406 407 // Check if it is possible to add more images. 408 $number_of_current_images = isset( $_POST['smfi_number_of_images'] ) && is_numeric( $_POST['smfi_number_of_images'] ) ? absint( $_POST['smfi_number_of_images'], 10 ) : -1; 409 $number_of_maximal_images = $this -> get_image_limit(); // returns -1 if there is not image limit. 410 $image_limit_exist = $number_of_maximal_images !== -1; 411 if( $image_limit_exist && $number_of_current_images >= $number_of_maximal_images ) { 412 // It is not allowed to add more images because the limit was reached. 413 throw new Exception( $this -> get_image_limit_exceeded_error_message_on_adding_image() . $number_of_current_images ); 414 } 415 416 // Get requested image by received image id and response the img html to the client. 399 417 $img_id = isset( $_POST['smfi_img_id'] ) && is_numeric( $_POST['smfi_img_id'] ) ? intval( $_POST['smfi_img_id'], 10 ) : -1; 400 418 if( $this -> validator -> is_valid_img_id( intval( $img_id, 10 ) ) ) { … … 410 428 } else { 411 429 // Get an invalid image id. 412 throw new Exception( "Received invalid image id during add new image request via ajax");430 throw new Exception( $this -> get_invalid_image_id_error_message_on_adding_image() ); 413 431 } 414 432 415 433 } catch( Exception $exception ) { 416 434 // Response error message to user. 417 $this -> send_ajax_error_as_json( );435 $this -> send_ajax_error_as_json( $exception -> getMessage() ); 418 436 } 419 437 } … … 479 497 * 480 498 * @since 1.0.0 499 * @since 1.1.0 Add hidden input field with the post id. It will be used during ajax calls to check if more images can be added to this post. 481 500 * 482 501 * @param WP_Post $post Current post object. … … 486 505 // Set nonce for this metabox. 487 506 wp_nonce_field( $this -> nonce_manager::NONCE_ACTION_METABOX . $post->ID, $this -> nonce_manager::NONCE_NAME_METABOX ); 507 508 /* 509 * Transmit the ID of the current post to the client. 510 * The ID is used on ajax calls to tell the server which specific post the call relates to. 511 * This is necessary because by default it is not known at ajax calls which post is currently being processed. 512 * The ID ist used, for example, to check if more images can be added to the corresponding post. 513 */ 514 echo '<input id= "smfi-post-id" name="post-id" type="hidden" value="' . esc_attr( get_the_ID() ) . '">'; 488 515 489 516 // Create and echo the html. … … 712 739 713 740 /** 714 * Returns the public API which can be used by external programs to work with the featured images plugin.715 * 716 * @since 1.0.0 717 * 718 * @return SMFI_Public_API The API.741 * Returns the meta key which is used inside the database for storing the image data. 742 * 743 * @since 1.0.0 744 * 745 * @return string Meta key. 719 746 */ 720 747 public function get_db_meta_key() { … … 860 887 } 861 888 862 863 889 /** 864 890 * Returns the error message for the failed changing of an image. … … 872 898 $errorDesc = esc_html__( 873 899 'Changing the image failed.' , 900 'simple-multiple-featured-images' ); 901 902 return 'Simple Multiple Featured Images Plugin: ' . $errorDesc . ' ' . $this -> get_default_error_instruction(); 903 } 904 905 /** 906 * Returns the error message for an exceeded image limit during adding a new image. 907 * 908 * @since 1.1.0 909 * 910 * @return string The escaped and translated error message. 911 */ 912 private function get_image_limit_exceeded_error_message_on_adding_image() { 913 914 $errorDesc = esc_html__( 915 'Adding a picture failed because the limit of the maximum possible pictures is reached. To add more pictures, the limit must be raised. ' , 916 'simple-multiple-featured-images' ); 917 918 return 'Simple Multiple Featured Images Plugin: ' . $errorDesc . ' ' . $this -> get_default_error_instruction(); 919 } 920 921 /** 922 * Returns the error message for an invalid image id during adding a new image. 923 * 924 * @since 1.1.0 925 * 926 * @return string The escaped and translated error message. 927 */ 928 private function get_invalid_image_id_error_message_on_adding_image() { 929 930 $errorDesc = esc_html__( 931 'Adding picture failed because of an invalid picture id. ' , 932 'simple-multiple-featured-images' ); 933 934 return 'Simple Multiple Featured Images Plugin: ' . $errorDesc . ' ' . $this -> get_default_error_instruction(); 935 } 936 937 /** 938 * Returns the error message for an invalid post id during adding a new image. 939 * 940 * @since 1.1.0 941 * 942 * @return string The escaped and translated error message. 943 */ 944 private function get_invalid_post_id_error_message_on_adding_image() { 945 946 $errorDesc = esc_html__( 947 'Adding picture failed because of an invalid post id. ' , 948 'simple-multiple-featured-images' ); 949 950 return 'Simple Multiple Featured Images Plugin: ' . $errorDesc . ' ' . $this -> get_default_error_instruction(); 951 } 952 953 /** 954 * Returns the error message for an invalid nonce during adding a new image. 955 * 956 * @since 1.1.0 957 * 958 * @return string The escaped and translated error message. 959 */ 960 private function get_invalid_nonce_error_message_on_adding_image() { 961 962 $errorDesc = esc_html__( 963 'Adding picture failed because of a security issue. ' , 874 964 'simple-multiple-featured-images' ); 875 965 … … 936 1026 $title = esc_attr__( 'Add image', 'simple-multiple-featured-images' ) . '">' . esc_html__( 'Add image', 'simple-multiple-featured-images' ); 937 1027 $output .= '<button id= "smfi-add-new-img-btn" class="button" title="' . $title . '</button>'; 1028 $output .= '<input id= "smfi-img-limit" name="img-limit" type="hidden" value="' . esc_attr( $this -> get_image_limit() ) . '">'; 938 1029 $output .= '</div>'; 939 1030 return $output; … … 1073 1164 return '<input name="' . $this -> hidden_input_name_img_ids . '[]" type="hidden" value="' . esc_attr( $img_id ) . '">'; 1074 1165 } 1166 1167 /** 1168 * Returns the image limit. The addition of new featured images will be prevented when the limit is exceeded. 1169 * 1170 * @since 1.1.0 1171 * 1172 * @return int The image limit. 1173 */ 1174 private function get_image_limit() { 1175 1176 /* 1177 * By default there is not image limit. 1178 * It is possible to add as many pictures as you like. 1179 * This will is represented by -1. 1180 */ 1181 $default_img_limit = -1; 1182 1183 /** 1184 * Filters the image limit. 1185 * 1186 * @since 1.1.0 1187 * 1188 * @param array The number of featured images which should be added. 1189 */ 1190 $img_limit = apply_filters( 'smfi_img_limit', $default_img_limit ); 1191 return is_int ( $img_limit ) && $img_limit > 0 ? $img_limit : $default_img_limit; 1192 } 1075 1193 } 1076 1194
Note: See TracChangeset
for help on using the changeset viewer.