admin.php
1 month ago
base.php
1 month ago
data-importer.php
1 month ago
helpers.php
1 month ago
importer.php
1 month ago
manager.php
1 month ago
parser.php
1 month ago
post-data-importer.php
1 month ago
rest.php
1 month ago
updater.php
1 month ago
view.php
1 month ago
data-importer.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpai\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | use PMXI_Model_Record; |
| 8 | use wpdb; |
| 9 | |
| 10 | abstract class PMXI_Addon_Data_Importer { |
| 11 | |
| 12 | public PMXI_Model_Record $record; |
| 13 | public bool $is_cron = false; |
| 14 | public wpdb $wpdb; |
| 15 | public $logger_callback; |
| 16 | |
| 17 | public static $default_model = PMXI_Addon_Post_Data_Importer::class; |
| 18 | |
| 19 | public function __construct( $record ) { |
| 20 | $this->record = $record; |
| 21 | $this->wpdb = $GLOBALS['wpdb']; |
| 22 | } |
| 23 | |
| 24 | public function __get( $name ) { |
| 25 | if ( $name === 'options' ) { |
| 26 | // check if defined |
| 27 | |
| 28 | if ( isset( $this->record->options ) ) { |
| 29 | return $this->record->options; |
| 30 | } |
| 31 | |
| 32 | return $this->record->options; |
| 33 | } |
| 34 | |
| 35 | if ( $name === 'id' ) { |
| 36 | return $this->record->id; |
| 37 | } |
| 38 | |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | public function setCron( $is_cron ) { |
| 43 | $this->is_cron = $is_cron; |
| 44 | } |
| 45 | |
| 46 | public function setLogger( $logger ) { |
| 47 | $this->logger_callback = $logger; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Check if there is an addon that supports the specific post type |
| 52 | * |
| 53 | * @param $record |
| 54 | * |
| 55 | * @return PMXI_Addon_Data_Importer |
| 56 | */ |
| 57 | public static function create( $record = null ) { |
| 58 | |
| 59 | // Default model |
| 60 | $model = self::$default_model; |
| 61 | |
| 62 | // Check if the record has options set, if not, use the default model |
| 63 | if ( ! isset( $record->options ) ) { |
| 64 | return new $model( $record ); |
| 65 | } |
| 66 | |
| 67 | $found_addon = \Wpai\AddonAPI\PMXI_Addon_Manager::get_owner_addon_for_type( |
| 68 | $record->options |
| 69 | ); |
| 70 | |
| 71 | if ( $found_addon ) { |
| 72 | $model = $found_addon->getCustomImporter( $record->options ) ?? $model; |
| 73 | } |
| 74 | |
| 75 | return new $model( $record ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param string $message |
| 80 | */ |
| 81 | public function logger( $message ) { |
| 82 | if ( $this->logger_callback ) { |
| 83 | call_user_func( $this->logger_callback, $message ); |
| 84 | }else{ |
| 85 | // Default logger. |
| 86 | if($this->is_cron) { |
| 87 | $logger = function($m) { |
| 88 | print("<p>[". esc_html(gmdate("H:i:s")) ."] ". wp_all_import_filter_html_kses($m) ."</p>\n"); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_all_import_filter_html_kses sanitizes via wp_kses. |
| 89 | }; |
| 90 | }else{ |
| 91 | $logger = function ( $m ) { |
| 92 | echo "<div class='progress-msg'>[" . esc_html(gmdate( "H:i:s" )) . "] " . wp_all_import_filter_html_kses( $m ) . "</div>\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_all_import_filter_html_kses sanitizes via wp_kses. |
| 93 | flush(); |
| 94 | }; |
| 95 | } |
| 96 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 97 | $this->logger_callback = apply_filters('wp_all_import_logger', $logger); |
| 98 | |
| 99 | call_user_func( $this->logger_callback, $message ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | abstract public function get_record( $post_id ); |
| 104 | |
| 105 | abstract public function get_record_title( $articleData ); |
| 106 | |
| 107 | abstract public function get_record_meta( $post_id ); |
| 108 | |
| 109 | abstract public function upsert( $articleData, $custom_type_details ); |
| 110 | |
| 111 | abstract public function update_content( $post_id, $content ); |
| 112 | |
| 113 | abstract public function update_meta( $post_id, $meta_key, $meta_value ); |
| 114 | |
| 115 | abstract public function combine_data( $data ); |
| 116 | |
| 117 | abstract public function choose_data_to_update( $post_to_update, $post_to_update_id, $post_type, $taxonomies, &$articleData, $i ); |
| 118 | |
| 119 | abstract public function change_post_status_to_draft( $missing_post_id ); |
| 120 | |
| 121 | abstract public function delete_meta( $post_id, $meta_key ); |
| 122 | |
| 123 | abstract public function delete_records( $ids ); |
| 124 | |
| 125 | abstract public function delete_images( $post_id, $articleData, $images_bundle ); |
| 126 | |
| 127 | abstract public function delete_image_custom_field( $post_id, $meta_key ); |
| 128 | |
| 129 | abstract public function delete_record_not_present_in_file( $missing_post_id ); |
| 130 | |
| 131 | abstract public function get_missing_records( $ids, $missing_status, $missing_cf ); |
| 132 | |
| 133 | abstract public function move_missing_record_to_trash( $missing_post_id ); |
| 134 | } |