1

I am trying to develop a custom plugin using object oriented programming.I am trying to insert data using wp cron jobs.with out cron job the data insertion to the database is working.But when i trying to insert data into database using cron job its not working. even cron job is not running.I have two files one is main plugin file.other one is plugin functionalities.Below is my code.

Main file code below:

<?php
/* 
Plugin Name: Test Reviews1
Plugin URI: https://test.in/
Description: This Test Plugin. 
Version: 1.0 
Author: Test 
Author URI: https://test.in/ 
License: GPLv2 or later 

    */
    new test_plugin();
    class test_plugin{

        public function __construct(){

            $this->plugin_dir = plugins_url( '' , __FILE__ );
            include('inc/inc.php');
            $this->security = new hidemysite_security();



        }

    } 

Functionalities file code below :

class hidemysite_security{

 public function __construct() {
  if (is_admin()) {
    //register_activation_hook(__FILE__, array(&$this, 'activate'));
      add_action('init', array($this,'activate'));  
      add_action('init', array($this,'my_plugin_remove_database')); 


  }
     add_filter( 'cron_schedules',array($this, 'wpcron_intervals'));
     register_activation_hook(__FILE__, array( $this, 'wpcron_activation'));
     register_deactivation_hook( __FILE__, array( $this, 'wpcron_deactivation' ) );
     add_action( 'example_event', array( $this, 'get_reviews' ) );
}

public function activate() {
  global $wpdb;
  $table = $wpdb->prefix . 'review_data';
  $charset = $wpdb->get_charset_collate();
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table (
        review_id bigint(10) NOT NULL AUTO_INCREMENT,
        customer_name tinytext NOT NULL,
        location varchar(250) NOT NULL,
        image varchar(500) DEFAULT '' NOT NULL,
        description text NOT NULL,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        PRIMARY KEY  (review_id)
    ) $charset_collate;";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}

public function  wpcron_intervals( $schedules ) {

        // one minute

        $one_minute = array(
                        'interval' => 60,
                        'display' => 'One Minute'
                    );

        $schedules[ 'one_minute' ] = $one_minute;

        // five minutes

        $five_minutes = array(
                        'interval' => 300,
                        'display' => 'Five Minutes'
                    );

        $schedules[ 'five_minutes' ] = $five_minutes;

        // return data

        return $schedules;

    }
public function wpcron_activation() {

        if ( ! wp_next_scheduled( 'example_event' ) ) {

            wp_schedule_event( time(), 'five_minutes', 'example_event' );

        }

}    

public function wpcron_deactivation() {
        wp_clear_scheduled_hook( 'example_event' );

}

public function get_reviews(){
    global $wpdb;
   $wc_reviews = get_comments( 
        array( 
            'status' => 'approve', 
            'post_status' => 'publish', 
            'post_type' => 'product',
            'parent' => '0',
//          'post__in' => $product_id,
            'number' => 10,
//          'comment__not_in' => $exclude,
//          'post__not_in' => $exclude_product,

            'orderby' => 'date',
        // 'status' => 'approve',
         'order' => 'DESC',
         'date_query' => array(
                array(
                'after' => '30 days ago',
                  )
               )

            ) 
        );
    //var_dump($wc_reviews); 

    foreach($wc_reviews as $wc_review){
      echo $post_id =  $wc_review->comment_post_ID;
        $customer_name = get_comment_author($wc_review);
        $location = "VLC Australia";
       // echo $image = get_the_post_thumbnail($post_id, array( 150 , 150 ) );
        //$description = get_the_post_thumbnail($wc_review->comment_post_ID, array( 150 , 150 ) );
        $description = get_comment_text($wc_review);
        $featured_img_url = get_the_post_thumbnail_url($post_id,'full');
        $table_name = $wpdb->prefix . 'review_data';

       $wpdb->insert( 
            $table_name, 
            array( 
                'customer_name' => $customer_name, 
                'location' => $location, 
                'image' => $featured_img_url, 
                'description' => $description, 
                'time' => current_time('mysql'), 
            ) 
        );

    }

}     




//    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.