[wp-trac] [WordPress Trac] #39364: Introduce a trigger to handle a custom queue job and run them

WordPress Trac noreply at wordpress.org
Wed Dec 21 17:03:29 UTC 2016


#39364: Introduce a trigger to handle a custom queue job and run them
----------------------------------------+-----------------------------
 Reporter:  shivapoudel                 |      Owner:
     Type:  enhancement                 |     Status:  new
 Priority:  normal                      |  Milestone:  Awaiting Review
Component:  Upgrade/Install             |    Version:  4.7
 Severity:  normal                      |   Keywords:
  Focuses:  javascript, administration  |
----------------------------------------+-----------------------------
 While I was creating a plugin which imports theme demos. I have to utilize
 the `wp.updates` for AJAX way to import and delete the demo packs but
 unfortunately there are no any trigger which I can utilize to update the
 queue job for demo by using its action and data in
 `wp.updates.queueChecker`. As a fix I have introduced a trigger in a patch
 file :)

 Any procedure to the extend self-executing anynonmous function
 `wp.updates.queueChecker` below with only trigger `$document.trigger( 'wp-
 updates-queue-job', job );` is much appreciated :)

 {{{
 ( function( $, wp ) {
         var $document = $( document );

         wp = wp || {};

         /**
          * The WP Updates object.
          *
          * @type {object}
          */
         wp.updates = wp.updates || {};

         /**
          * Sends an Ajax request to the server to delete a demo.
          *
          * @param {object}             args
          * @param {string}             args.slug    Demo Pack.
          * @param {deleteDemoSuccess=} args.success Optional. Success
 callback. Default: wp.updates.deleteDemoSuccess
          * @param {deleteDemoError=}   args.error   Optional. Error
 callback. Default: wp.updates.deleteDemoError
          * @return {$.promise} A jQuery promise that represents the
 request,
          *                     decorated with an abort() method.
          */
         wp.updates.deleteDemo = function( args ) {
                 var $button = $( '.theme-actions .delete-demo' );

                 args = _.extend( {
                         success: wp.updates.deleteDemoSuccess,
                         error: wp.updates.deleteDemoError
                 }, args );

                 if ( $button && $button.html() !==
 wp.updates.l10n.deleting ) {
                         $button
                                 .data( 'originaltext', $button.html() )
                                 .text( wp.updates.l10n.deleting );
                 }

                 wp.a11y.speak( wp.updates.l10n.deleting, 'polite' );

                 // Remove previous error messages, if any.
                 $( '.theme-info .update-message' ).remove();

                 $document.trigger( 'wp-demo-deleting', args );

                 return wp.updates.ajax( 'delete-demo', args );
         };

         /**
          * Updates the UI appropriately after a successful demo deletion.
          *
          * @typedef {object} deleteDemoSuccess
          * @param {object} response      Response from the server.
          * @param {string} response.slug Slug of the demo that was
 deleted.
          */
         wp.updates.deleteDemoSuccess = function( response ) {
                 wp.a11y.speak( wp.updates.l10n.deleted, 'polite' );

                 $document.trigger( 'wp-demo-delete-success', response );
         };

         /**
          * Updates the UI appropriately after a failed demo deletion.
          *
          * @typedef {object} deleteDemoError
          * @param {object} response              Response from the server.
          * @param {string} response.slug         Slug of the demo to be
 deleted.
          * @param {string} response.errorCode    Error code for the error
 that occurred.
          * @param {string} response.errorMessage The error that occurred.
          */
         wp.updates.deleteDemoError = function( response ) {
                 var $button      = $( '.theme-actions .delete-demo' ),
                         errorMessage =
 wp.updates.l10n.deleteFailed.replace( '%s', response.errorMessage ),
                         $message     = wp.updates.adminNotice( {
                                 className: 'update-message notice-error
 notice-alt',
                                 message:   errorMessage
                         } );

                 if ( wp.updates.maybeHandleCredentialError( response,
 'delete-demo' ) ) {
                         return;
                 }

                 $( '.theme-info .theme-description' ).before( $message );

                 $button.html( $button.data( 'originaltext' ) );

                 wp.a11y.speak( errorMessage, 'assertive' );

                 $document.trigger( 'wp-demo-delete-error', response );
         };

         /**
          * Pulls available jobs from the queue and runs them.
          */
         wp.updates.queueChecker = function() {
                 var job;

                 if ( wp.updates.ajaxLocked || ! wp.updates.queue.length )
 {
                         return;
                 }

                 job = wp.updates.queue.shift();

                 // Handle a queue job.
                 switch ( job.action ) {
                         case 'install-plugin':
                                 wp.updates.installPlugin( job.data );
                                 break;

                         case 'update-plugin':
                                 wp.updates.updatePlugin( job.data );
                                 break;

                         case 'delete-plugin':
                                 wp.updates.deletePlugin( job.data );
                                 break;

                         case 'install-theme':
                                 wp.updates.installTheme( job.data );
                                 break;

                         case 'update-theme':
                                 wp.updates.updateTheme( job.data );
                                 break;

                         case 'delete-theme':
                                 wp.updates.deleteTheme( job.data );
                                 break;

                         default:
                                 break;
                 }

                 $document.trigger( 'wp-updates-queue-job', job );
         };

 })( jQuery, window.wp );

 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39364>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list