Contracts
9 months ago
BatchProcessor.php
9 months ago
Migration.php
9 months ago
ProcessByWcMigrator.php
9 months ago
QuizAttemptMigrator.php
9 months ago
ProcessByWcMigrator.php
162 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Since Tutor 3.0.0 all wc earnings store in tutor_earnings table with 'Tutor' in process_by column. |
| 4 | * This migration will update all wc earnings process_by column to 'woocommerce'. |
| 5 | * |
| 6 | * @package Tutor |
| 7 | * @author Themeum <support@themeum.com> |
| 8 | * @link https://themeum.com |
| 9 | * @since 3.8.2 |
| 10 | */ |
| 11 | |
| 12 | namespace Tutor\Migrations; |
| 13 | |
| 14 | use TUTOR\Earnings; |
| 15 | use Tutor\Helpers\QueryHelper; |
| 16 | use Tutor\Migrations\Contracts\BulkProcessor; |
| 17 | |
| 18 | /** |
| 19 | * Class ProcessByWcMigrator |
| 20 | * |
| 21 | * @since 3.8.2 |
| 22 | */ |
| 23 | class ProcessByWcMigrator extends BatchProcessor implements BulkProcessor { |
| 24 | /** |
| 25 | * Name of the migration |
| 26 | * |
| 27 | * @since 3.8.2 |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $name = 'Process By WC Migration'; |
| 32 | |
| 33 | /** |
| 34 | * Action |
| 35 | * |
| 36 | * @since 3.8.2 |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | protected $action = 'process_by_wc_migrator'; |
| 41 | |
| 42 | /** |
| 43 | * Batch size |
| 44 | * |
| 45 | * @since 3.8.2 |
| 46 | * |
| 47 | * @var integer |
| 48 | */ |
| 49 | protected $batch_size = 1000; |
| 50 | |
| 51 | /** |
| 52 | * Schedule interval. |
| 53 | * |
| 54 | * @since 3.8.2 |
| 55 | * |
| 56 | * @var integer |
| 57 | */ |
| 58 | protected $schedule_interval = 10; |
| 59 | |
| 60 | /** |
| 61 | * Get the total count of the data to be processed |
| 62 | * |
| 63 | * @since 3.8.2 |
| 64 | * |
| 65 | * @return int |
| 66 | */ |
| 67 | protected function get_total_items() : int { |
| 68 | $primary_table = 'tutor_earnings te'; |
| 69 | $joining_tables = array( |
| 70 | array( |
| 71 | 'table' => 'wc_orders_meta wcom', |
| 72 | 'on' => 'te.order_id = wcom.order_id', |
| 73 | 'type' => 'INNER', |
| 74 | ), |
| 75 | ); |
| 76 | |
| 77 | $where = array( |
| 78 | 'te.process_by' => Earnings::PROCESS_BY_TUTOR, |
| 79 | 'wcom.meta_key' => '_is_tutor_order_for_course', |
| 80 | ); |
| 81 | |
| 82 | $total_items = QueryHelper::get_joined_count( |
| 83 | $primary_table, |
| 84 | $joining_tables, |
| 85 | $where, |
| 86 | array(), |
| 87 | 'te.earning_id' |
| 88 | ); |
| 89 | |
| 90 | return $total_items; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get items to batch process. |
| 95 | * |
| 96 | * @since 3.8.2 |
| 97 | * |
| 98 | * @param int $offset offset. |
| 99 | * @param int $limit limit. |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | protected function get_items( $offset, $limit ) : array { |
| 104 | $primary_table = 'tutor_earnings te'; |
| 105 | $joining_tables = array( |
| 106 | array( |
| 107 | 'table' => 'wc_orders_meta wcom', |
| 108 | 'on' => 'te.order_id = wcom.order_id', |
| 109 | 'type' => 'INNER', |
| 110 | ), |
| 111 | ); |
| 112 | |
| 113 | $where = array( |
| 114 | 'te.process_by' => Earnings::PROCESS_BY_TUTOR, |
| 115 | 'wcom.meta_key' => '_is_tutor_order_for_course', |
| 116 | ); |
| 117 | |
| 118 | $response = QueryHelper::get_joined_data( |
| 119 | $primary_table, |
| 120 | $joining_tables, |
| 121 | array( 'te.*' ), |
| 122 | $where, |
| 123 | array(), |
| 124 | 'te.earning_id', |
| 125 | $limit, |
| 126 | $offset, |
| 127 | 'DESC' |
| 128 | ); |
| 129 | |
| 130 | $items = $response['total_count'] > 0 ? $response['results'] : array(); |
| 131 | |
| 132 | return $items; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Process a batch of items |
| 137 | * |
| 138 | * @since 3.8.2 |
| 139 | * |
| 140 | * @param array $items items. |
| 141 | * |
| 142 | * @return void |
| 143 | */ |
| 144 | public function process_items( $items ) : void { |
| 145 | $ids = wp_list_pluck( $items, 'earning_id' ); |
| 146 | $data = array( 'process_by' => Earnings::PROCESS_BY_WOOCOMMERCE ); |
| 147 | $where = array( 'earning_id' => $ids ); |
| 148 | QueryHelper::update( 'tutor_earnings', $data, $where ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * On migration complete event. |
| 153 | * |
| 154 | * @since 3.8.2 |
| 155 | * |
| 156 | * @return void |
| 157 | */ |
| 158 | protected function on_complete() { |
| 159 | error_log( 'Process by WC migration completed!' ); |
| 160 | } |
| 161 | } |
| 162 |