| 1 |
<?php |
| 2 |
/** |
| 3 |
* Divi Importer |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
* @since 1.1.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! class_exists( 'CartFlows_Importer_Divi' ) ) : |
| 10 |
|
| 11 |
/** |
| 12 |
* CartFlows Import Divi |
| 13 |
* |
| 14 |
* @since 1.1.1 |
| 15 |
*/ |
| 16 |
class CartFlows_Importer_Divi { |
| 17 |
|
| 18 |
/** |
| 19 |
* Instance |
| 20 |
* |
| 21 |
* @since 1.1.1 |
| 22 |
* @access private |
| 23 |
* @var object Class object. |
| 24 |
*/ |
| 25 |
private static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initiator |
| 29 |
* |
| 30 |
* @since 1.1.1 |
| 31 |
* @return object initialized object of class. |
| 32 |
*/ |
| 33 |
public static function get_instance() { |
| 34 |
|
| 35 |
if ( ! isset( self::$instance ) ) { |
| 36 |
self::$instance = new self; |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor |
| 43 |
* |
| 44 |
* @since 1.1.1 |
| 45 |
*/ |
| 46 |
public function __construct() { |
| 47 |
add_action( 'admin_head', array( $this, 're_process_data_flow' ) ); |
| 48 |
add_action( 'admin_head', array( $this, 're_process_data_step' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Rest Data. |
| 53 |
* |
| 54 |
* @param integer $post_id Post ID. |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
function reset_data( $post_id ) { |
| 58 |
|
| 59 |
$content = get_post_meta( $post_id, 'divi_content_processed', true ); |
| 60 |
|
| 61 |
if ( ! empty( $content ) ) { |
| 62 |
|
| 63 |
$content = CartFlows_Importer::get_instance()->get_content( $content ); |
| 64 |
|
| 65 |
// Update post content. |
| 66 |
wp_update_post( |
| 67 |
array( |
| 68 |
'ID' => $post_id, |
| 69 |
'post_content' => $content, |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Re process data. |
| 77 |
*/ |
| 78 |
function re_process_data_flow() { |
| 79 |
wcf()->logger->import_log( ' Without batch started.. ' ); |
| 80 |
|
| 81 |
if ( CARTFLOWS_FLOW_POST_TYPE !== get_post_type() ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
global $pagenow; |
| 86 |
|
| 87 |
if ( 'post.php' !== $pagenow ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Is Highlight step then return! |
| 92 |
// Because, It process only for the first flow import. |
| 93 |
if ( isset( $_GET['highlight-step-id'] ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$post_id = get_the_ID(); |
| 98 |
|
| 99 |
/* If Imported Flow */ |
| 100 |
if ( 'yes' !== get_post_meta( $post_id, 'cartflows_imported_flow', true ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// wcf()->logger->import_log( ' Reprocessing Divi Data.. ' . $post_id );. |
| 105 |
$flow_processed = get_post_meta( $post_id, 'wcf_divi_flow_data_processed', true ); |
| 106 |
|
| 107 |
if ( ! $flow_processed ) { |
| 108 |
|
| 109 |
$steps = get_post_meta( $post_id, 'wcf-steps', true ); |
| 110 |
$steps_count = count( $steps ); |
| 111 |
$steps_remaing = $steps_count; |
| 112 |
|
| 113 |
foreach ( $steps as $key => $step ) { |
| 114 |
|
| 115 |
$step_processed = get_post_meta( $step['id'], 'wcf_divi_step_data_processed', true ); |
| 116 |
|
| 117 |
if ( ! $step_processed ) { |
| 118 |
|
| 119 |
$content = get_post_meta( $step['id'], 'divi_content_processed', true ); |
| 120 |
// It means that batch is not complete yet. |
| 121 |
if ( ! empty( $content ) ) { |
| 122 |
$this->reset_data( $step['id'] ); |
| 123 |
|
| 124 |
wcf()->logger->import_log( ' Processing without batch.. ' . $step['id'] ); |
| 125 |
|
| 126 |
// Step processed! |
| 127 |
update_post_meta( $step['id'], 'wcf_divi_step_data_processed', true ); |
| 128 |
|
| 129 |
$steps_remaing--; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// All step data processed. |
| 135 |
if ( 0 === $steps_remaing ) { |
| 136 |
wcf()->logger->import_log( ' ----------- FLOW COMPLETE ----------------- ' . $post_id ); |
| 137 |
update_post_meta( $post_id, 'wcf_divi_flow_data_processed', true ); |
| 138 |
} |
| 139 |
} else { |
| 140 |
wcf()->logger->import_log( ' Already processed.. ' ); |
| 141 |
} |
| 142 |
|
| 143 |
wcf()->logger->import_log( ' Without batch end.. ' ); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Re process data for step. |
| 149 |
*/ |
| 150 |
function re_process_data_step() { |
| 151 |
|
| 152 |
wcf()->logger->import_log( ' Without batch started.. ' ); |
| 153 |
|
| 154 |
if ( CARTFLOWS_STEP_POST_TYPE !== get_post_type() ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
$post_id = get_the_ID(); |
| 159 |
|
| 160 |
/* If Imported Step */ |
| 161 |
if ( 'yes' !== get_post_meta( $post_id, 'cartflows_imported_step', true ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$step_processed = get_post_meta( $post_id, 'wcf_divi_step_data_processed', true ); |
| 166 |
|
| 167 |
if ( ! $step_processed ) { |
| 168 |
|
| 169 |
$content = get_post_meta( $post_id, 'divi_content_processed', true ); |
| 170 |
// It means that batch is not complete yet. |
| 171 |
if ( ! empty( $content ) ) { |
| 172 |
$this->reset_data( $post_id ); |
| 173 |
|
| 174 |
wcf()->logger->import_log( ' Processing without batch.. ' . $post_id ); |
| 175 |
|
| 176 |
// Step processed! |
| 177 |
update_post_meta( $post_id, 'wcf_divi_step_data_processed', true ); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Update post meta. |
| 184 |
* |
| 185 |
* @param integer $post_id Post ID. |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public function import_single_post( $post_id = 0 ) { |
| 189 |
|
| 190 |
// Download and replace images. |
| 191 |
$content = get_post_meta( $post_id, 'divi_content', true ); |
| 192 |
|
| 193 |
if ( empty( $content ) ) { |
| 194 |
wcf()->logger->import_log( '(✕) Not have "Divi" Data. Post content is empty!' ); |
| 195 |
} else { |
| 196 |
|
| 197 |
wcf()->logger->import_log( '(✓) Processing Request..' ); |
| 198 |
|
| 199 |
// Update hotlink images. |
| 200 |
$content = CartFlows_Importer::get_instance()->get_content( $content ); |
| 201 |
|
| 202 |
// Save processed data. |
| 203 |
update_post_meta( $post_id, 'divi_content_processed', $content ); |
| 204 |
|
| 205 |
// Delete temporary meta key. |
| 206 |
wcf()->logger->import_log( '(✓) Process Complete' ); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Initialize class object with 'get_instance()' method |
| 214 |
*/ |
| 215 |
CartFlows_Importer_Divi::get_instance(); |
| 216 |
|
| 217 |
endif; |
| 218 |
|