Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

cron - Schedule Action on WordPress

I have a question about the schedule actions on WordPress. I've already set up some cron jobs on my website, but now I am going through hard times. I have a custom plugin on my website that allows me to import n contents from external source via api. On the plugin dashboard, I have this button that allows me to delete all contents in bulk without leaving any track on WordPress database.

button

Since I need to do that operation every single day, I wish if there's a way to set an auto click for this button inside wp-admin every 24 hours or if I can set a cronjobs for this.

<button
                class="btn btn-sm wplc__btn--muted mb-3"
                v-bind:disabled="deleteContents.processing"
                v-on:click.prevent="runDeleteContents()"
              >
              <spinner v-if="deleteContents.displaySpinner" size="14" color="#ccc" v-bind:key="site.id + '__deleteContentsLoading'"></spinner>
                {{ deleteContents.text }}
              </button>

and

 runBulkDeleteContents: function() {
      console.log( 'bulk delete contents' );
      this.$http
        .post(
          wplcSites.ajax.url,
          {
            action: 'wplc_sites_delete_contents',
            nonce: wplcSites.ajax.nonce,
            site_id: this.site.id
          },
          { emulateJSON: true }
        )
        .then(
          function( successResponse ) {
            console.log( successResponse );
          },
          function( deleteContentsError ) {
          }
        )
        .then( function() {} );
    },
runDeleteContents: function() {
      this.deleteContents.processing = true;
      this.deleteContents.displaySpinner = true;
      this.deleteContents.text = 'Deleting all contents';
      this.$http
        .post(
          wplcSite.ajax.url,
          {
            action: 'wplc_sites_get_site_posts_ids',
            nonce: wplcSites.ajax.nonce,
            site_id: this.site.id
          },
          { emulateJSON: true }
        )
        .then(
          function( successResponse ) {
            var self = this;
            var maxConcurrentAjaxCalls = 10;
            if ( true === successResponse.body.success ) {
              var contentsPostsIds$ = rxjs.from( successResponse.body.data );
              contentssPostsIds$.pipe(
                rxjs.operators.bufferCount( 25 ),
                rxjs.operators.mergeMap(
                  function( postIds ) {
                    return rxjs.from( self.deleteContentsChunk( postIds ) );
                  },
                  null,
                  maxConcurrentAjaxCalls
              )
              ).subscribe(
                function( contentsChunkDeleted ) {},
                function( error ) {
                  console.error( error );
                  this.deleteContents.processing = false;
                  self.deleteContents.displaySpinner = false;
                  self.deleteContents.text = '? Error while deleting contents';
                  setTimeout( function() {
                    self.deleteContents.text = 'Delete all contents';
                  }, 1000 );
                },
                function() {
                  self.deleteContents.processing = false;
                  self.deleteContents.displaySpinner = false;
                  self.deleteContents.text = '? All contents deleted successfully';
                  setTimeout( function() {
                    self.deleteContents.text = 'Delete all contents';
                    self.site.has_contents_posts = false;
                  }, 1000 );
                }
              );
            } else {}
          },
          function( deleteContentsError ) {
          }
        )
        .then( function() {} );
    },

deleteContentsChunk: function( postIds ) {
      var response = {};
      return this.$http
        .post(
          wplcSites.ajax.url,
          {
            action: 'wplc_site_delete_contents_chunk',
            nonce: wplcSites.ajax.nonce,
            post_ids: postIds
          },
          { emulateJSON: true }
        )
        .then(
          function( deleteContentsSuccess ) {
            if ( true === deleteContentsSuccess.body.success ) {
              response = { success: true };
            } else {
              response = { success: false };
            }
          },
          function( deleteContentsError ) {
            response = { success: false };
          }
        )
        .then( function() {
          return response;
        });
    },

All of these two pieces of code are inside a JS file inside the plugin folder. There is a way to schedule that deletion?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...