From 83aac9862502922b431ee5eace28cf2392e673f2 Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Mon, 23 Sep 2019 16:08:47 +0545 Subject: [PATCH 01/11] Fix multilang values for directly nested attributes --- .wde_nextRelease.json | 7 ++- src/classes/Import_Base.php | 94 ++++++++++++++++++++++++++++++++---- src/classes/Import_Posts.php | 34 +------------ src/classes/Import_Terms.php | 37 ++------------ 4 files changed, 96 insertions(+), 76 deletions(-) diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 8d6505b..60a8627 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -1,3 +1,8 @@ { - "changes": [] + "changes": [ + { + "type": "fixed", + "message": "Multilang values for directly nested attributes. eg `meta_input`. Also works if some directly nested attributes have translations, and others not." + } + ] } \ No newline at end of file diff --git a/src/classes/Import_Base.php b/src/classes/Import_Base.php index 9b1e00e..6839d38 100644 --- a/src/classes/Import_Base.php +++ b/src/classes/Import_Base.php @@ -28,6 +28,8 @@ public function __construct( $objects ){ $this->setup_import_data( $objects ); + $this->prepare_import_data(); + $this->insert_objects(); return $this; @@ -35,6 +37,37 @@ public function __construct( $objects ){ abstract protected function setup_import_data( $file_data ); + protected function prepare_import_data() { + // classify_atts_by_validy for wp_insert_post wp_insert_term function + // and fix_insert_args + foreach( $this->objects as $object_i => $object ) { + if ( $object['is_wpml_import'] ) { + foreach( $object['atts'] as $lang => $atts ) { + if ( 'all' === $lang ) + continue; + + // merge atts recursive for all into current atts_by_lang + $atts = array_merge_recursive( + $atts, + utils\Arr::get( $this->objects, $object_i . '.atts.all', array() ) + ); + + $atts = $this->classify_atts_by_validy( $atts, $object['is_wpml_import'] ); + + $atts = $this->fix_insert_args( $atts ); + + $this->objects[$object_i]['atts'][$lang] = apply_filters( "yaim_{$this->type}_atts", $atts, $lang, $object ); + } + } else { + $atts = $this->classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); + + $atts = $this->fix_insert_args( $atts ); + + $this->objects[$object_i]['atts']['all'] = apply_filters( "yaim_{$this->type}_atts", $atts, 'all', $object ); + } + } + } + abstract protected function classify_atts_by_validy( $object_atts, $use_deferred = false ); protected function fix_insert_args( $atts ) { @@ -48,8 +81,10 @@ protected function insert_objects() { } foreach( $this->objects as $i => $object ) { - if ( is_wp_error( $object['atts'] ) ) + if ( is_wp_error( $object['atts'] ) ) { + $this->log[] = 'ERROR: ' . $object['atts']->get_error_message(); continue; + } if ( $object['is_wpml_import'] ) { $this->insert_object_wmpl( $i, $object ); } else { @@ -87,16 +122,20 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = if ( null !== $default_lang ) $atts[$default_lang] = array(); + $may_be_nested_keys = array( + 'meta_input', + ); + foreach( $object_raw_data as $key => $attr ) { if ( is_array( $attr ) ) { - $keys_starts_wpml_ = array_unique( array_map( function( $k ) { - return substr( $k, 0, strlen( 'wpml_' ) ) === 'wpml_'; - }, array_keys( $attr ) ) ); - if ( count( $keys_starts_wpml_ ) > 1 ) // something wrong, mixed language fields + $is_wpml_attr = $this->is_wpml_attr( $key, array_keys( $attr ) ); + if ( is_wp_error( $is_wpml_attr ) ) { + $this->log[] = 'ERROR: ' . $is_wpml_attr->get_error_message(); continue; + } - if ( $keys_starts_wpml_[0] ) { // all keys start with wpml_ + if ( $is_wpml_attr ) { foreach( $attr as $lang => $val ) { $lang = str_replace( 'wpml_', '', $lang ); if ( in_array( $lang, $this->active_langs ) ) { @@ -106,11 +145,37 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = $atts[$lang][$key] = $val; } } else { - // something wrong, lang not active + $this->log[] = "ERROR: \$lang={$lang} not acive"; + } + } + } else { + foreach( $attr as $n_key => $n_attr ) { + + if ( is_array( $n_attr ) ) { + + $is_wpml_attr = $this->is_wpml_attr( $n_key, array_keys( $n_attr ) ); + if ( is_wp_error( $is_wpml_attr ) ) { + $this->log[] = 'ERROR: ' . $is_wpml_attr->get_error_message(); + continue; + } + + if ( $is_wpml_attr ) { + foreach( $n_attr as $lang => $val ) { + $lang = str_replace( 'wpml_', '', $lang ); + if ( in_array( $lang, $this->active_langs ) ) { + $atts[$lang][$key][$n_key] = $val; + } else { + $this->log[] = "ERROR: \$lang={$lang} not acive"; + } + } + } else { // is normal field + $atts['all'][$key][$n_key] = $n_attr; + } + + } else { // is normal field + $atts['all'][$key][$n_key] = $n_attr; } } - } else { // is normal field - $atts['all'][$key] = $attr; } } else { // is normal field @@ -120,6 +185,17 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = return $atts; } + protected function is_wpml_attr( $key, $keys ) { + $keys_starts_wpml_ = array_unique( array_map( function( $k ) { + return substr( $k, 0, strlen( 'wpml_' ) ) === 'wpml_'; + }, $keys ) ); + + if ( count( $keys_starts_wpml_ ) > 1 ) + return new \WP_Error( 'attr_mixed_language', sprintf( __( '"%s" has translated and non-translated input.', 'yaim' ), $key ) ); + + return $keys_starts_wpml_[0]; + } + /** * Public getter method for retrieving protected/private variables * @param string $field Field to retrieve diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index 353d19f..87f6e6e 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -28,12 +28,8 @@ protected function setup_import_data( $objects ) { } } - // check if is_wpml_import for this object - $is_wpml_import = - $is_wpml_import = $this->is_wpml_import( $object_raw_data ); - $this->objects[$object_i] = array( - 'is_wpml_import' => $is_wpml_import, + 'is_wpml_import' => $this->is_wpml_import( $object_raw_data ), 'inserted' => array(), 'atts' => $this->classify_atts_by_lang( $object_raw_data, array( 'post_type', @@ -43,34 +39,6 @@ protected function setup_import_data( $objects ) { } - // classify_atts_by_validy for wp_insert_post function - // and fix_insert_args - foreach( $this->objects as $object_i => $object ) { - if ( $object['is_wpml_import'] ) { - foreach( $object['atts'] as $lang => $atts ) { - if ( 'all' === $lang ) - continue; - - // merge atts for all into current atts_by_lang - $atts = array_merge( - $atts, - utils\Arr::get( $this->objects, $object_i . '.atts.all', array() ) - ); - - $atts = $this->classify_atts_by_validy( $atts, $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts'][$lang] = apply_filters( "yaim_{$this->type}_atts", $atts, $lang, $object ); // ??? should add type as param - } - } else { - $atts = $this->classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts']['all'] = apply_filters( "yaim_{$this->type}_atts", $atts, 'all', $object ); - } - } } protected function classify_atts_by_validy( $object_atts, $use_deferred = false ) { diff --git a/src/classes/Import_Terms.php b/src/classes/Import_Terms.php index 5064bc7..ba621e4 100644 --- a/src/classes/Import_Terms.php +++ b/src/classes/Import_Terms.php @@ -18,45 +18,16 @@ protected function setup_import_data( $objects ) { // classify_atts_by_lang and check is_wpml_import foreach( $objects as $object_i => $object_raw_data ) { - // check if is_wpml_import for this object - $is_wpml_import = $this->is_wpml_import( $object_raw_data ); - $this->objects[$object_i] = array( - 'is_wpml_import' => $is_wpml_import, + 'is_wpml_import' => $this->is_wpml_import( $object_raw_data ), 'inserted' => array(), - 'atts' => $this->classify_atts_by_lang( $object_raw_data, array() ), + 'atts' => $this->classify_atts_by_lang( $object_raw_data, array( + 'taxonomy', + ) ), ); } - // classify_atts_by_validy for wp_insert_term function - // and fix_insert_args - foreach( $this->objects as $object_i => $object ) { - if ( $object['is_wpml_import'] ) { - foreach( $object['atts'] as $lang => $atts ) { - if ( 'all' === $lang ) - continue; - - // merge atts for all into current atts_by_lang - $atts = array_merge( - $atts, - utils\Arr::get( $this->objects, $object_i . '.atts.all', array() ) - ); - - $atts = $this->classify_atts_by_validy( $atts, $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts'][$lang] = apply_filters( "yaim_{$this->type}_atts", $atts, $lang, $object ); - } - } else { - $atts = $this->classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts']['all'] = apply_filters( "yaim_{$this->type}_atts", $atts, 'all', $object ); - } - } } protected function classify_atts_by_validy( $object_atts, $use_deferred = false ) { From 3ad1f612947854a72b4f2b4a48e1ee81b75c4c81 Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Mon, 23 Sep 2019 16:09:28 +0545 Subject: [PATCH 02/11] Handle `meta_input` field for Term imports --- .wde_nextRelease.json | 4 ++++ src/classes/Import_Terms.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 60a8627..499c6ae 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -3,6 +3,10 @@ { "type": "fixed", "message": "Multilang values for directly nested attributes. eg `meta_input`. Also works if some directly nested attributes have translations, and others not." + }, + { + "type": "added", + "message": "Handle `meta_input` field for Term imports" } ] } \ No newline at end of file diff --git a/src/classes/Import_Terms.php b/src/classes/Import_Terms.php index ba621e4..4fb7734 100644 --- a/src/classes/Import_Terms.php +++ b/src/classes/Import_Terms.php @@ -141,6 +141,35 @@ protected function insert_object_wmpl( $i, $object ) { ); } + // handle meta_input + foreach( $object['atts'] as $lang => $atts_by_lang ) { + if ( 'all' === $lang ) + continue; + + $meta_input = utils\Arr::get( $atts_by_lang,'custom_args.meta_input' ); + + if ( ! $meta_input ) + continue; + + $object_id = utils\Arr::get( $object, 'inserted.' . $lang, false ); + + if ( ! $object_id ) + continue; + + foreach( $meta_input as $meta_key => $meta_val ) { + $updated = update_term_meta( $object_id, $meta_key, $meta_val ); + + if ( is_numeric( $updated ) && $updated == ( int ) $updated ) { + $this->log[$object_id . '_' . $meta_key] = "Updated {$this->type} \$id={$object_id} added \$meta_key={$meta_key} \$meta_id={$updated}"; + } elseif ( $updated ) { + $this->log[$object_id . '_' . $meta_key] = "Updated {$this->type} \$id={$object_id} updated \$meta_key={$meta_key}"; + } else { + $this->log[$object_id . '_' . $meta_key] = "ERROR updating {$object_id}: couldn't add \$meta_key={$meta_key}"; + continue; + } + } + } + if ( isset( $object_id ) && is_numeric( $object_id ) ) $this->log[$object_id . '_after'] = ''; From 2549e72f6ddfb91422df02ddd1235f103485084a Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Mon, 23 Sep 2019 17:28:05 +0545 Subject: [PATCH 03/11] Use `wpautop`for certain fields --- .wde_nextRelease.json | 4 ++++ src/classes/Import_Base.php | 23 ++++++++++++++--------- src/classes/Import_Posts.php | 5 +++++ src/classes/Import_Terms.php | 4 ++++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 499c6ae..89a650b 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -7,6 +7,10 @@ { "type": "added", "message": "Handle `meta_input` field for Term imports" + }, + { + "type": "added", + "message": "Use `wpautop`for certain fields. Fields are filtered by \"yaim_{$this->type}_autop_keys\" filter" } ] } \ No newline at end of file diff --git a/src/classes/Import_Base.php b/src/classes/Import_Base.php index 6839d38..7dfcca5 100644 --- a/src/classes/Import_Base.php +++ b/src/classes/Import_Base.php @@ -17,6 +17,8 @@ abstract class Import_Base { protected $active_langs = array(); + protected $autop_keys = array(); + protected $log = array(); public function __construct( $objects ){ @@ -122,10 +124,6 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = if ( null !== $default_lang ) $atts[$default_lang] = array(); - $may_be_nested_keys = array( - 'meta_input', - ); - foreach( $object_raw_data as $key => $attr ) { if ( is_array( $attr ) ) { @@ -142,7 +140,7 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = if ( in_array( $key, $not_translatable ) ) { return new \WP_Error( 'attr_not_translatable', sprintf( __( '"%s" is not translatable.', 'yaim' ), $key ) ); } else { - $atts[$lang][$key] = $val; + $atts[$lang][$key] = $this->maybe_autop( $key, $val ); } } else { $this->log[] = "ERROR: \$lang={$lang} not acive"; @@ -163,28 +161,35 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = foreach( $n_attr as $lang => $val ) { $lang = str_replace( 'wpml_', '', $lang ); if ( in_array( $lang, $this->active_langs ) ) { - $atts[$lang][$key][$n_key] = $val; + $atts[$lang][$key][$n_key] = $this->maybe_autop( $n_key, $val ); } else { $this->log[] = "ERROR: \$lang={$lang} not acive"; } } } else { // is normal field - $atts['all'][$key][$n_key] = $n_attr; + $atts['all'][$key][$n_key] = $this->maybe_autop( $n_key, $n_attr ); } } else { // is normal field - $atts['all'][$key][$n_key] = $n_attr; + $atts['all'][$key][$n_key] = $this->maybe_autop( $n_key, $n_attr ); } } } } else { // is normal field - $atts['all'][$key] = $attr; + $atts['all'][$key] = $this->maybe_autop( $key, $attr ); } } return $atts; } + protected function maybe_autop( $key, $val ) { + $autop_keys = apply_filters( "yaim_{$this->type}_autop_keys", $this->autop_keys ); + return in_array( $key, $autop_keys ) + ? wpautop( $val ) + : $val; + } + protected function is_wpml_attr( $key, $keys ) { $keys_starts_wpml_ = array_unique( array_map( function( $k ) { return substr( $k, 0, strlen( 'wpml_' ) ) === 'wpml_'; diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index 87f6e6e..a4efbb7 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -13,6 +13,11 @@ class Import_Posts extends Import_Base { protected $type = 'post'; + protected $autop_keys = array( + 'post_content', + 'post_excerpt', + ); + protected function setup_import_data( $objects ) { // classify_atts_by_lang and check is_wpml_import diff --git a/src/classes/Import_Terms.php b/src/classes/Import_Terms.php index 4fb7734..c8b534e 100644 --- a/src/classes/Import_Terms.php +++ b/src/classes/Import_Terms.php @@ -13,6 +13,10 @@ class Import_Terms extends Import_Base { protected $type = 'term'; + protected $autop_keys = array( + 'description', + ); + protected function setup_import_data( $objects ) { // classify_atts_by_lang and check is_wpml_import From fd6b94ed9dfec46fbe1ca1a3d9ef454eb717f63e Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Tue, 24 Sep 2019 14:18:01 +0545 Subject: [PATCH 04/11] Handle p2p connections for post import --- .wde_nextRelease.json | 4 + src/classes/Import_Posts.php | 137 ++++++++++++++++++++++++++++++----- 2 files changed, 124 insertions(+), 17 deletions(-) diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 89a650b..2ae4e85 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -11,6 +11,10 @@ { "type": "added", "message": "Use `wpautop`for certain fields. Fields are filtered by \"yaim_{$this->type}_autop_keys\" filter" + }, + { + "type": "added", + "message": "Handle p2p connections for post import" } ] } \ No newline at end of file diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index a4efbb7..a1732ae 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -203,7 +203,39 @@ protected function insert_object_wmpl( $i, $object ) { } - // do the deferred stuff + $this->update_object_deferred( $i, $object ); + + $this->update_object_p2p( $i, $object ); + + if ( isset( $object_id ) ) + $this->log[$object_id . '_after'] = ''; + + do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + + } + + protected function insert_object( $i, $object ) { + $object_id = wp_insert_post( array_merge( + utils\Arr::get( $object, 'atts.all.insert_args', array() ), + utils\Arr::get( $object, 'atts.all.deferred_insert_args', array() ) + ) ); + + if ( is_wp_error( $object_id ) ) { + $this->log[] = 'ERROR: ' . $object_id->get_error_message(); + return; + } + + $this->objects[$i]['inserted']['all'] = $object_id; + + $this->update_object_p2p( $i, $object ); + + $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + + do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + } + + protected function update_object_deferred( $i, $object ) { + foreach( $object['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; @@ -237,30 +269,101 @@ protected function insert_object_wmpl( $i, $object ) { $this->log[$object_id . '_up'] = "Updated {$this->type} \$id={$object_id} \$args=[" . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . "] and wpml should have done the sync"; } + } - if ( isset( $object_id ) ) - $this->log[$object_id . '_after'] = ''; + protected function update_object_p2p( $i, $object ) { + if ( class_exists( 'P2P_Connection_Type_Factory' ) ) { + foreach( $object['atts'] as $lang => $atts_by_lang ) { + if ( 'all' === $lang ) + continue; - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + $p2p = utils\Arr::get( $atts_by_lang,'custom_args.p2p' ); - } + if ( ! $p2p ) + continue; - protected function insert_object( $i, $object ) { - $object_id = wp_insert_post( array_merge( - utils\Arr::get( $object, 'atts.all.insert_args', array() ), - utils\Arr::get( $object, 'atts.all.deferred_insert_args', array() ) - ) ); + $object_id = utils\Arr::get( $object, 'inserted.' . $lang, false ); - if ( is_wp_error( $object_id ) ) { - $this->log[] = 'ERROR: ' . $object_id->get_error_message(); - return; - } + if ( ! $object_id ) + continue; - $this->objects[$i]['inserted']['all'] = $object_id; + foreach( $p2p as $ctype_name => $conns ) { - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + $ctype = \P2P_Connection_Type_Factory::get_instance( $ctype_name ); - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], null ); + $object_side = false; + $object_post_type = get_post_type( $object_id ); + $both_sides_are_posttype = true; + $conn_post_type = false; + foreach( $ctype->side as $direction => $side ) { + if ( ! $side instanceof \P2P_Side_Post ) { + $both_sides_are_posttype = false; + continue; + } + if ( in_array( $object_post_type, utils\Arr::get( $side->query_vars, 'post_type' ) ) ) { + $object_side = ! $object_side + ? $direction + : $object_side; + } else { + $conn_post_type = ! $conn_post_type + ? utils\Arr::get( $side->query_vars, 'post_type.0' ) + : $conn_post_type; + } + } + + if ( ! $both_sides_are_posttype ) { + $this->log[] = "WARNING updating {$object_id}: currently p2p connections are only supported, if both connection sides represent a posttype"; + continue; + } + + if ( ! $object_side ) { + $this->log[] = "ERROR updating {$object_id}: No side of \$connection_type={$ctype_name} represents \$posttype={$object_post_type} "; + continue; + } + + $connected_post_ids = array(); + foreach( $conns as $conn_k => $conn_v ) { + + $conn_post_slug = is_array( $conn_v ) || ( is_string( $conn_v ) && empty( $conn_v ) ) + ? $conn_k + : $conn_v; + + $get_post_args = array( + 'name' => $conn_post_slug, + 'posts_per_page' => 1, + 'post_type' => $conn_post_type, + 'fields' => 'ids', + ); + $get_post_args = class_exists( 'SitePress' ) ? array_merge( $get_post_args, array( + 'lang' => apply_filters( 'wpml_current_language', null ), + 'suppress_filters' => false, + ) ) : $get_post_args; + + $conn_post_id = utils\Arr::get( get_posts( $get_post_args ), '0', false ); + + if ( ! $conn_post_id ) { + $this->log[] = "WARNING updating {$object_id}: can't create p2p connection, can't find post with \$slug={$conn_post_slug}"; + continue; + } + + // create connection + $p2p_id = $ctype->connect( + 'from' === $object_side ? $object_id : $conn_post_id, + 'to' === $object_side ? $object_id : $conn_post_id, + is_array( $conn_v ) ? $conn_v : array() + ); + + if ( is_wp_error( $p2p_id ) ) { + $this->log[] = 'ERROR: ' . $p2p_id->get_error_message(); + continue; + } + $connected_post_ids[] = $conn_post_id; + } + + $this->log[$object_id . '_p2p_' . $conn_post_id] = "Updated {$this->type} \$id={$object_id} p2p connected with post \$ids=[" . implode( ', ', $connected_post_ids ) . "] \$ctype_name={$ctype_name}"; + } + } + } } } \ No newline at end of file From 408c3515ce6ec91f808361dfe3c6aa2a6b4382ad Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Tue, 24 Sep 2019 16:42:01 +0545 Subject: [PATCH 05/11] More stable log Before log was saved after import. Now log can be saved to db anytime. New $option_key='yaim_log' Start time is saved separate $option_key='yaim_log_start' --- .wde_nextRelease.json | 4 ++ src/classes/Import_Base.php | 17 +++++---- src/classes/Import_Posts.php | 32 ++++++++-------- src/classes/Import_Terms.php | 26 ++++++------- src/classes/Log.php | 70 +++++++++++++++++++++++++++++++++++ src/classes/Settings_Page.php | 41 +++++++------------- 6 files changed, 125 insertions(+), 65 deletions(-) create mode 100644 src/classes/Log.php diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 2ae4e85..7839f98 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -15,6 +15,10 @@ { "type": "added", "message": "Handle p2p connections for post import" + }, + { + "type": "changed", + "message": "More stable log" } ] } \ No newline at end of file diff --git a/src/classes/Import_Base.php b/src/classes/Import_Base.php index 7dfcca5..769e85e 100644 --- a/src/classes/Import_Base.php +++ b/src/classes/Import_Base.php @@ -19,15 +19,17 @@ abstract class Import_Base { protected $autop_keys = array(); - protected $log = array(); + protected $log; - public function __construct( $objects ){ + public function __construct( $objects, $log ){ $this->active_langs = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ); $this->active_langs = null === $this->active_langs ? $this->active_langs : array_map( function( $lang ) { return $lang['language_code']; }, $this->active_langs ); + $this->log = $log; + $this->setup_import_data( $objects ); $this->prepare_import_data(); @@ -84,7 +86,7 @@ protected function insert_objects() { foreach( $this->objects as $i => $object ) { if ( is_wp_error( $object['atts'] ) ) { - $this->log[] = 'ERROR: ' . $object['atts']->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $object['atts']->get_error_message() ); continue; } if ( $object['is_wpml_import'] ) { @@ -92,6 +94,7 @@ protected function insert_objects() { } else { $this->insert_object( $i, $object ); } + $this->log->save(); } if ( class_exists( 'SitePress' ) ) { @@ -129,7 +132,7 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = $is_wpml_attr = $this->is_wpml_attr( $key, array_keys( $attr ) ); if ( is_wp_error( $is_wpml_attr ) ) { - $this->log[] = 'ERROR: ' . $is_wpml_attr->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); continue; } @@ -143,7 +146,7 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = $atts[$lang][$key] = $this->maybe_autop( $key, $val ); } } else { - $this->log[] = "ERROR: \$lang={$lang} not acive"; + $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); } } } else { @@ -153,7 +156,7 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = $is_wpml_attr = $this->is_wpml_attr( $n_key, array_keys( $n_attr ) ); if ( is_wp_error( $is_wpml_attr ) ) { - $this->log[] = 'ERROR: ' . $is_wpml_attr->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); continue; } @@ -163,7 +166,7 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = if ( in_array( $lang, $this->active_langs ) ) { $atts[$lang][$key][$n_key] = $this->maybe_autop( $n_key, $val ); } else { - $this->log[] = "ERROR: \$lang={$lang} not acive"; + $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); } } } else { // is normal field diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index a1732ae..f106bd3 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -159,7 +159,7 @@ protected function insert_object_wmpl( $i, $object ) { $object_id = wp_insert_post( $atts_by_lang['insert_args'] ); if ( is_wp_error( $object_id ) ) { - $this->log[] = 'ERROR: ' . $object_id->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); continue; } @@ -169,7 +169,7 @@ protected function insert_object_wmpl( $i, $object ) { $object['inserted'][$lang] = $object_id; $this->objects[$i]['inserted'][$lang] = $object_id; - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); // object_trid of first inserted object $object_trid = null === $object_trid @@ -190,8 +190,8 @@ protected function insert_object_wmpl( $i, $object ) { $lang ); - $this->log[$object_id . '_in'] .= " \$lang={$lang} \$translation_id={$translation_id}"; - $this->log[$object_id . '_in'] .= $original_id === $object_id ? '' : " as translation for \$id={$original_id}"; + $this->log->add_entry( " \$lang={$lang} \$translation_id={$translation_id}", $object_id . '_in' ); + $this->log->add_entry( $original_id === $object_id ? '' : " as translation for \$id={$original_id}", $object_id . '_in' ); do_action( "yaim_{$this->type}_wpml_language_set", $object_id, @@ -208,9 +208,7 @@ protected function insert_object_wmpl( $i, $object ) { $this->update_object_p2p( $i, $object ); if ( isset( $object_id ) ) - $this->log[$object_id . '_after'] = ''; - - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id, $this->log ); // ??? needs $i } @@ -221,7 +219,7 @@ protected function insert_object( $i, $object ) { ) ); if ( is_wp_error( $object_id ) ) { - $this->log[] = 'ERROR: ' . $object_id->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); return; } @@ -229,9 +227,9 @@ protected function insert_object( $i, $object ) { $this->update_object_p2p( $i, $object ); - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id, $this->log ); } protected function update_object_deferred( $i, $object ) { @@ -263,11 +261,11 @@ protected function update_object_deferred( $i, $object ) { $updated = wp_update_post( $args, true ); if ( is_wp_error( $updated ) ) { - $this->log[] = "ERROR updating {$object_id}: " . $updated->get_error_message(); + $this->log->add_entry( "ERROR updating {$object_id}: " . $updated->get_error_message() ); continue; } - $this->log[$object_id . '_up'] = "Updated {$this->type} \$id={$object_id} \$args=[" . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . "] and wpml should have done the sync"; + $this->log->add_entry( "Updated {$this->type} \$id={$object_id} \$args=[" . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . "] and wpml should have done the sync", $object_id . '_up' ); } } @@ -312,12 +310,12 @@ protected function update_object_p2p( $i, $object ) { } if ( ! $both_sides_are_posttype ) { - $this->log[] = "WARNING updating {$object_id}: currently p2p connections are only supported, if both connection sides represent a posttype"; + $this->log->add_entry( "WARNING updating {$object_id}: currently p2p connections are only supported, if both connection sides represent a posttype" ); continue; } if ( ! $object_side ) { - $this->log[] = "ERROR updating {$object_id}: No side of \$connection_type={$ctype_name} represents \$posttype={$object_post_type} "; + $this->log->add_entry( "ERROR updating {$object_id}: No side of \$connection_type={$ctype_name} represents \$posttype={$object_post_type} " ); continue; } @@ -342,7 +340,7 @@ protected function update_object_p2p( $i, $object ) { $conn_post_id = utils\Arr::get( get_posts( $get_post_args ), '0', false ); if ( ! $conn_post_id ) { - $this->log[] = "WARNING updating {$object_id}: can't create p2p connection, can't find post with \$slug={$conn_post_slug}"; + $this->log->add_entry( "WARNING updating {$object_id}: can't create p2p connection, can't find post with \$slug={$conn_post_slug}" ); continue; } @@ -354,13 +352,13 @@ protected function update_object_p2p( $i, $object ) { ); if ( is_wp_error( $p2p_id ) ) { - $this->log[] = 'ERROR: ' . $p2p_id->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $p2p_id->get_error_message() ); continue; } $connected_post_ids[] = $conn_post_id; } - $this->log[$object_id . '_p2p_' . $conn_post_id] = "Updated {$this->type} \$id={$object_id} p2p connected with post \$ids=[" . implode( ', ', $connected_post_ids ) . "] \$ctype_name={$ctype_name}"; + $this->log->add_entry( "Updated {$this->type} \$id={$object_id} p2p connected with post \$ids=[" . implode( ', ', $connected_post_ids ) . "] \$ctype_name={$ctype_name}", $object_id . '_p2p_' . $conn_post_id ); } } } diff --git a/src/classes/Import_Terms.php b/src/classes/Import_Terms.php index c8b534e..48202a9 100644 --- a/src/classes/Import_Terms.php +++ b/src/classes/Import_Terms.php @@ -100,7 +100,7 @@ protected function insert_object_wmpl( $i, $object ) { $term_tax_ids = wp_insert_term( $name, $taxonomy, $args ); if ( is_wp_error( $term_tax_ids ) ) { - $this->log[] = 'ERROR: ' . $term_tax_ids->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $term_tax_ids->get_error_message() ); continue; } @@ -112,7 +112,7 @@ protected function insert_object_wmpl( $i, $object ) { $object['inserted'][$lang] = $object_id; $this->objects[$i]['inserted'][$lang] = $object_id; - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); // object_trid of first inserted object $object_trid = null === $object_trid @@ -133,8 +133,8 @@ protected function insert_object_wmpl( $i, $object ) { $lang ); - $this->log[$object_id . '_in'] .= " \$lang={$lang} \$translation_id={$translation_id}"; - $this->log[$object_id . '_in'] .= $original_id === $object_id ? '' : " as translation for \$id={$original_id}"; + $this->log->add_entry( " \$lang={$lang} \$translation_id={$translation_id}", $object_id . '_in' ); + $this->log->add_entry( $original_id === $object_id ? '' : " as translation for \$id={$original_id}", $object_id . '_in' ); do_action( "yaim_{$this->type}_wpml_language_set", $object_id, @@ -164,20 +164,19 @@ protected function insert_object_wmpl( $i, $object ) { $updated = update_term_meta( $object_id, $meta_key, $meta_val ); if ( is_numeric( $updated ) && $updated == ( int ) $updated ) { - $this->log[$object_id . '_' . $meta_key] = "Updated {$this->type} \$id={$object_id} added \$meta_key={$meta_key} \$meta_id={$updated}"; + $this->log->add_entry( "Updated {$this->type} \$id={$object_id} added \$meta_key={$meta_key} \$meta_id={$updated}", $object_id . '_' . $meta_key ); + } elseif ( $updated ) { - $this->log[$object_id . '_' . $meta_key] = "Updated {$this->type} \$id={$object_id} updated \$meta_key={$meta_key}"; + $this->log->add_entry( "Updated {$this->type} \$id={$object_id} updated \$meta_key={$meta_key}", $object_id . '_' . $meta_key ); } else { - $this->log[$object_id . '_' . $meta_key] = "ERROR updating {$object_id}: couldn't add \$meta_key={$meta_key}"; + $this->log->add_entry( "ERROR updating {$object_id}: couldn't add \$meta_key={$meta_key}", $object_id . '_' . $meta_key ); continue; } } } - if ( isset( $object_id ) && is_numeric( $object_id ) ) - $this->log[$object_id . '_after'] = ''; - - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + if ( isset( $object_id ) ) + do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); } @@ -189,15 +188,14 @@ protected function insert_object( $i, $object ) { $term_tax_ids = wp_insert_term( $name, $taxonomy, $args ); if ( is_wp_error( $term_tax_ids ) ) { - $this->log[] = 'ERROR: ' . $term_tax_ids->get_error_message(); + $this->log->add_entry( 'ERROR: ' . $term_tax_ids->get_error_message(); return; } $object_id = $term_tax_ids['term_id']; $this->objects[$i]['inserted']['all'] = $object_id; - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; - + $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); do_action( "yaim_{$this->type}_inserted", $this->objects[$i], null ); } diff --git a/src/classes/Log.php b/src/classes/Log.php new file mode 100644 index 0000000..26ca41a --- /dev/null +++ b/src/classes/Log.php @@ -0,0 +1,70 @@ +start = date( 'Y-m-d H:i:s' ); + update_option( self::$option_key . '_start', $this->start ); + + $this->add_entries( array( $this->start, '' ) ); + + } + + public function add_entries( $msgs = array(), $key = null ){ + foreach( $msgs as $msg ) { + $this->add_entry( $msg ); + } + } + + public function add_entry( $msg = '', $key = null ){ + if ( null === $key ) { + $this->msgs[] = $msg; + } else { + if ( array_key_exists( $key, $this->msgs ) ) { + $this->msgs[$key] .= $msg; + + } else { + $this->msgs[$key] = $msg; + } + } + } + + public function get(){ + return $this->msgs; + } + + public static function get_from_db(){ + $msgs = get_option( self::$option_key ); + $last = get_option( self::$option_key . '_start' ); + return array( + 'msgs' => $msgs, + 'last_start' => get_option( self::$option_key . '_start' ), + 'last_log_saved' => count( $msgs ) > 0 && $msgs[0] === $last, + ); + } + + public function save(){ + $this->add_entries( array( + date( 'Y-m-d H:i:s' ) . ' Save log to db $option_key=' . self::$option_key, + '', + ) ); + update_option( self::$option_key, $this->msgs ); + } + +} + diff --git a/src/classes/Settings_Page.php b/src/classes/Settings_Page.php index 49be126..c0e119a 100644 --- a/src/classes/Settings_Page.php +++ b/src/classes/Settings_Page.php @@ -83,12 +83,6 @@ public function options_page_metabox() { 'options_cb' => array( $this, 'options_cb' ), ) ); - // empty field, useful to save the log later - $cmb->add_field( array( - 'id' => 'log', - 'type' => 'select', // select because it will sanitize and save array - 'render_row_cb' => array( $this, 'render_empty_field' ), - ) ); } function render_empty_field( $field_args, $field ) {} @@ -107,6 +101,7 @@ public function options_cb( $field ) { // Do some processing just before the fileds are saved public function process_fields( $cmb, $cmb_id ) { + $this->log = new Log(); $file = utils\Arr::get( $cmb->data_to_save, 'file' ); @@ -121,15 +116,13 @@ public function process_fields( $cmb, $cmb_id ) { foreach( $file_data as $type => $objects ) { switch( $type ) { case 'posts': - $importer = new Import_Posts( $objects ); - $cmb->data_to_save['log'] = array_merge( $cmb->data_to_save['log'], $importer->log ); + $importer = new Import_Posts( $objects, $this->log ); break; case 'terms': - $importer = new Import_Terms( $objects ); - $cmb->data_to_save['log'] = array_merge( $cmb->data_to_save['log'], $importer->log ); + $importer = new Import_Terms( $objects, $this->log ); break; default: - $cmb->data_to_save['log'][] = "ERROR: import for {$type} not supported"; + $this->log->add_entry( "ERROR: import for {$type} not supported" ); } } @@ -161,23 +154,17 @@ public function message_cb( $cmb, $args ) { if ( empty( $args['should_notify'] ) ) return; - $log = utils\Arr::get( get_option( $this->cmb_id ), 'log', array() ); - - if ( ! $args['is_updated'] ) - $log[] = '

??? maybe something went wrong

'; - - $log[] = 'Log saved to db $option_name=' . $this->cmb_id; + $log = Log::get_from_db(); - $args['message'] = implode( '
', array_map( function( $msg ) { - return is_string( $msg ) && strlen( $msg ) > 0 - ? $msg . ';' - : $msg; - }, $log ) ); - - - $args['type'] = strpos( $args['message'], 'ERROR' ) !== false - ? 'notice-warning' - : $args['type']; + if ( $log['last_log_saved'] ) { + $args['message'] = implode( '
', $log['msgs'] ); + $args['type'] = strpos( $args['message'], 'ERROR' ) !== false + ? 'notice-warning' + : 'updated'; + } else { + $args['message'] = 'Something went wrong and no log saved'; + $args['type'] = 'error'; + } add_settings_error( $args['setting'], $args['code'], $args['message'], $args['type'] ); } From 087485b896cf7f5aa9f2b0ff1d5316160c7fc9c9 Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Wed, 25 Sep 2019 11:55:36 +0545 Subject: [PATCH 06/11] Add comoser package a5hleyrich/wp-background-processing --- .wde_nextRelease.json | 4 ++ composer.json | 7 +++- composer.lock | 40 +++++++++++++++++++- grunt/hooked/addComposerCopyTasks.js | 13 +++++++ grunt/hooked/addMustangostangSpycCopyTask.js | 26 ------------- 5 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 grunt/hooked/addComposerCopyTasks.js delete mode 100644 grunt/hooked/addMustangostangSpycCopyTask.js diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 7839f98..780bc4b 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -19,6 +19,10 @@ { "type": "changed", "message": "More stable log" + }, + { + "type": "added", + "message": "a5hleyrich/wp-background-processing" } ] } \ No newline at end of file diff --git a/composer.json b/composer.json index 677e4d0..bc7aa6e 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,13 @@ { - "require": {}, + "require": { + }, "require-dev": { "croox/wp-dev-env-frame": "0.7.2", "composer/installers": "*", "dealerdirect/phpcodesniffer-composer-installer": "0.5.0", "wptrt/wpthemereview": "0.1.0", "mustangostang/spyc": "^0.6.2", + "a5hleyrich/wp-background-processing": "^1.0", "cmb2/cmb2": "^2.6" }, "repositories": [ @@ -22,7 +24,8 @@ "extra": { "installer-paths": { "vendor/{$vendor}/{$name}": [ - "cmb2/cmb2" + "cmb2/cmb2", + "a5hleyrich/wp-background-processing" ] } } diff --git a/composer.lock b/composer.lock index 9bebb1b..c359eb2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,10 +4,46 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "e52664cfe48b7a33049877924aed3ce5", - "content-hash": "90664eb1f11969a80a01b252380cc32e", + "hash": "c505c74cce338d28b0c18e3c2f5d61a3", + "content-hash": "f56f221cb6f35a1c3cdda81f7d24ff81", "packages": [], "packages-dev": [ + { + "name": "a5hleyrich/wp-background-processing", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/A5hleyRich/wp-background-processing.git", + "reference": "1f070aab5058dbaf45d5435a343033ddd8a641b1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/A5hleyRich/wp-background-processing/zipball/1f070aab5058dbaf45d5435a343033ddd8a641b1", + "reference": "1f070aab5058dbaf45d5435a343033ddd8a641b1", + "shasum": "" + }, + "require": { + "php": ">=5.2" + }, + "type": "library", + "autoload": { + "classmap": [ + "classes/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-only" + ], + "authors": [ + { + "name": "Ashley Rich", + "email": "hello@ashleyrich.com" + } + ], + "description": "WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks.", + "time": "2018-02-12 09:24:05" + }, { "name": "cmb2/cmb2", "version": "v2.6.0", diff --git a/grunt/hooked/addComposerCopyTasks.js b/grunt/hooked/addComposerCopyTasks.js new file mode 100644 index 0000000..ea22816 --- /dev/null +++ b/grunt/hooked/addComposerCopyTasks.js @@ -0,0 +1,13 @@ + +const addComposerCopyTask = require('../addComposerCopyTask'); + +const addComposerCopyTasks = grunt => { + + [ + 'a5hleyrich/wp-background-processing', + 'mustangostang/spyc', + ].map( name => addComposerCopyTask( grunt, { name } ) ); + +} + +module.exports = addComposerCopyTasks; diff --git a/grunt/hooked/addMustangostangSpycCopyTask.js b/grunt/hooked/addMustangostangSpycCopyTask.js deleted file mode 100644 index e509a3d..0000000 --- a/grunt/hooked/addMustangostangSpycCopyTask.js +++ /dev/null @@ -1,26 +0,0 @@ - -const addComposerCopyTask = require('../addComposerCopyTask'); - -const addMustangostangSpycCopyTask = grunt => { - - [ - // 'jms/serializer', - // 'jms/metadata', - // 'jms/parser-lib', - // 'doctrine/annotations', - // 'doctrine/instantiator', - // 'doctrine/lexer', - // 'hoa/compiler', - // 'phpcollection/phpcollection', - // 'phpoption/phpoption', - // 'symfony/polyfill-ctype', - // 'symfony/yaml', - - - - 'mustangostang/spyc', - ].map( name => addComposerCopyTask( grunt, { name } ) ); - -} - -module.exports = addMustangostangSpycCopyTask; From b43b9366637e63582be3e28c55610554fcec9c67 Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Wed, 25 Sep 2019 12:27:34 +0545 Subject: [PATCH 07/11] First try to use background processes for post import works! temporary disabled - logs - terms import --- .../{Log.php => Admin_Message_Log.php} | 2 +- src/classes/Import_Base.php | 196 +++++++++--------- src/classes/Import_Posts.php | 131 ++++++------ src/classes/Settings_Page.php | 61 ++++-- 4 files changed, 214 insertions(+), 176 deletions(-) rename src/classes/{Log.php => Admin_Message_Log.php} (98%) diff --git a/src/classes/Log.php b/src/classes/Admin_Message_Log.php similarity index 98% rename from src/classes/Log.php rename to src/classes/Admin_Message_Log.php index 26ca41a..a244035 100644 --- a/src/classes/Log.php +++ b/src/classes/Admin_Message_Log.php @@ -9,7 +9,7 @@ use croox\wde\utils; -class Log { +class Admin_Message_Log { protected $msgs = array(); diff --git a/src/classes/Import_Base.php b/src/classes/Import_Base.php index 769e85e..53f1d17 100644 --- a/src/classes/Import_Base.php +++ b/src/classes/Import_Base.php @@ -9,104 +9,101 @@ use croox\wde\utils; -abstract class Import_Base { +abstract class Import_Base extends \WP_Background_Process { protected $objects = array(); - protected $type = ''; // eg. post, term + protected static $type = ''; // eg. post, term - protected $active_langs = array(); - - protected $autop_keys = array(); + protected static $autop_keys = array(); protected $log; - public function __construct( $objects, $log ){ - - $this->active_langs = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ); - $this->active_langs = null === $this->active_langs ? $this->active_langs : array_map( function( $lang ) { - return $lang['language_code']; - }, $this->active_langs ); + // public function __construct(){ + // parent::__construct(); + // } + public function add_logger( $log ) { $this->log = $log; + } - $this->setup_import_data( $objects ); + protected function task( $object_raw_data ) { - $this->prepare_import_data(); + $object = static::setup_import_data( $object_raw_data ); - $this->insert_objects(); + $object = static::prepare_import_data( $object ); - return $this; - } + if ( class_exists( 'SitePress' ) ) { + $current_lang = apply_filters( 'wpml_current_language', null ); + do_action( 'wpml_switch_language', apply_filters( 'wpml_default_language', null ) ); + } - abstract protected function setup_import_data( $file_data ); + if ( ! is_wp_error( $object['atts'] ) ) { - protected function prepare_import_data() { - // classify_atts_by_validy for wp_insert_post wp_insert_term function - // and fix_insert_args - foreach( $this->objects as $object_i => $object ) { if ( $object['is_wpml_import'] ) { - foreach( $object['atts'] as $lang => $atts ) { - if ( 'all' === $lang ) - continue; + $object = $this->insert_object_wmpl( $object ); + } else { + $object = $this->insert_object( $object ); + } - // merge atts recursive for all into current atts_by_lang - $atts = array_merge_recursive( - $atts, - utils\Arr::get( $this->objects, $object_i . '.atts.all', array() ) - ); + } else { + // $this->log->add_entry( 'ERROR: ' . $object['atts']->get_error_message() ); + } - $atts = $this->classify_atts_by_validy( $atts, $object['is_wpml_import'] ); + // $this->log->add_entry( 'done' ); + // $this->log->save(); - $atts = $this->fix_insert_args( $atts ); + if ( class_exists( 'SitePress' ) ) { + do_action( 'wpml_switch_language', $current_lang, null ); + } - $this->objects[$object_i]['atts'][$lang] = apply_filters( "yaim_{$this->type}_atts", $atts, $lang, $object ); - } - } else { - $atts = $this->classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); + return false; + } - $atts = $this->fix_insert_args( $atts ); + abstract protected static function setup_import_data( $object ); - $this->objects[$object_i]['atts']['all'] = apply_filters( "yaim_{$this->type}_atts", $atts, 'all', $object ); - } - } - } + // classify_atts_by_validy for wp_insert_post wp_insert_term function + // and fix_insert_args + protected static function prepare_import_data( $object ) { + if ( $object['is_wpml_import'] ) { + foreach( $object['atts'] as $lang => $atts ) { + if ( 'all' === $lang ) + continue; - abstract protected function classify_atts_by_validy( $object_atts, $use_deferred = false ); + // merge atts recursive for all into current atts_by_lang + $atts = array_merge_recursive( + $atts, + utils\Arr::get( $object, 'atts.all', array() ) + ); - protected function fix_insert_args( $atts ) { - return $atts; - } + $atts = static::classify_atts_by_validy( $atts, $object['is_wpml_import'] ); - protected function insert_objects() { - if ( class_exists( 'SitePress' ) ) { - $current_lang = apply_filters( 'wpml_current_language', null ); - do_action( 'wpml_switch_language', apply_filters( 'wpml_default_language', null ) ); - } + $atts = static::fix_insert_args( $atts ); - foreach( $this->objects as $i => $object ) { - if ( is_wp_error( $object['atts'] ) ) { - $this->log->add_entry( 'ERROR: ' . $object['atts']->get_error_message() ); - continue; + $object['atts'][$lang] = apply_filters( "yaim_" . static::$type . "_atts", $atts, $lang, $object ); } - if ( $object['is_wpml_import'] ) { - $this->insert_object_wmpl( $i, $object ); - } else { - $this->insert_object( $i, $object ); - } - $this->log->save(); - } + } else { + $atts = static::classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); - if ( class_exists( 'SitePress' ) ) { - do_action( 'wpml_switch_language', $current_lang, null ); + $atts = static::fix_insert_args( $atts ); + + $object['atts']['all'] = apply_filters( "yaim_" . static::$type . "_atts", $atts, 'all', $object ); } + + return $object; + } + + abstract protected static function classify_atts_by_validy( $object_atts, $use_deferred = false ); + + protected static function fix_insert_args( $atts ) { + return $atts; } - abstract protected function insert_object( $i, $object ); + abstract protected function insert_object( $object ); - abstract protected function insert_object_wmpl( $i, $object ); + abstract protected function insert_object_wmpl( $object ); - protected function is_wpml_import( $object_raw_data ) { + protected static function is_wpml_import( $object_raw_data ) { $is_wpml_import = false; foreach( $object_raw_data as $key => $attr ) { if ( $is_wpml_import ) @@ -117,7 +114,7 @@ protected function is_wpml_import( $object_raw_data ) { return $is_wpml_import; } - protected function classify_atts_by_lang( $object_raw_data, $not_translatable = array() ) { + protected static function classify_atts_by_lang( $object_raw_data, $not_translatable = array() ) { $atts = array( 'all' => array() ); @@ -130,23 +127,23 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = foreach( $object_raw_data as $key => $attr ) { if ( is_array( $attr ) ) { - $is_wpml_attr = $this->is_wpml_attr( $key, array_keys( $attr ) ); + $is_wpml_attr = static::is_wpml_attr( $key, array_keys( $attr ) ); if ( is_wp_error( $is_wpml_attr ) ) { - $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); + // $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); continue; } if ( $is_wpml_attr ) { foreach( $attr as $lang => $val ) { $lang = str_replace( 'wpml_', '', $lang ); - if ( in_array( $lang, $this->active_langs ) ) { + if ( in_array( $lang, static::get_active_langs() ) ) { if ( in_array( $key, $not_translatable ) ) { return new \WP_Error( 'attr_not_translatable', sprintf( __( '"%s" is not translatable.', 'yaim' ), $key ) ); } else { - $atts[$lang][$key] = $this->maybe_autop( $key, $val ); + $atts[$lang][$key] = static::maybe_autop( $key, $val ); } } else { - $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); + // $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); } } } else { @@ -154,46 +151,46 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = if ( is_array( $n_attr ) ) { - $is_wpml_attr = $this->is_wpml_attr( $n_key, array_keys( $n_attr ) ); + $is_wpml_attr = static::is_wpml_attr( $n_key, array_keys( $n_attr ) ); if ( is_wp_error( $is_wpml_attr ) ) { - $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); + // $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); continue; } if ( $is_wpml_attr ) { foreach( $n_attr as $lang => $val ) { $lang = str_replace( 'wpml_', '', $lang ); - if ( in_array( $lang, $this->active_langs ) ) { - $atts[$lang][$key][$n_key] = $this->maybe_autop( $n_key, $val ); + if ( in_array( $lang, static::get_active_langs() ) ) { + $atts[$lang][$key][$n_key] = static::maybe_autop( $n_key, $val ); } else { - $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); + // $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); } } } else { // is normal field - $atts['all'][$key][$n_key] = $this->maybe_autop( $n_key, $n_attr ); + $atts['all'][$key][$n_key] = static::maybe_autop( $n_key, $n_attr ); } } else { // is normal field - $atts['all'][$key][$n_key] = $this->maybe_autop( $n_key, $n_attr ); + $atts['all'][$key][$n_key] = static::maybe_autop( $n_key, $n_attr ); } } } } else { // is normal field - $atts['all'][$key] = $this->maybe_autop( $key, $attr ); + $atts['all'][$key] = static::maybe_autop( $key, $attr ); } } return $atts; } - protected function maybe_autop( $key, $val ) { - $autop_keys = apply_filters( "yaim_{$this->type}_autop_keys", $this->autop_keys ); + protected static function maybe_autop( $key, $val ) { + $autop_keys = apply_filters( "yaim_" . static::$type . "_autop_keys", static::$autop_keys ); return in_array( $key, $autop_keys ) ? wpautop( $val ) : $val; } - protected function is_wpml_attr( $key, $keys ) { + protected static function is_wpml_attr( $key, $keys ) { $keys_starts_wpml_ = array_unique( array_map( function( $k ) { return substr( $k, 0, strlen( 'wpml_' ) ) === 'wpml_'; }, $keys ) ); @@ -204,20 +201,27 @@ protected function is_wpml_attr( $key, $keys ) { return $keys_starts_wpml_[0]; } - /** - * Public getter method for retrieving protected/private variables - * @param string $field Field to retrieve - * @return mixed Field value or exception is thrown - */ - public function __get( $field ) { - if ( in_array( $field, array( - 'objects', - 'type', - 'log', - ), true ) ) { - return $this->{$field}; - } - throw new Exception( 'Invalid property: ' . $field ); + protected static function get_active_langs() { + $active_langs = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ); + return is_array( $active_langs ) + ? array_map( function( $lang ) { return $lang['language_code']; }, $active_langs ) + : array(); } + // /** + // * Public getter method for retrieving protected/private variables + // * @param string $field Field to retrieve + // * @return mixed Field value or exception is thrown + // */ + // public function __get( $field ) { + // if ( in_array( $field, array( + // 'objects', + // 'type', + // 'log', + // ), true ) ) { + // return $this->{$field}; + // } + // throw new Exception( 'Invalid property: ' . $field ); + // } + } \ No newline at end of file diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index f106bd3..72e3e6b 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -11,42 +11,40 @@ class Import_Posts extends Import_Base { - protected $type = 'post'; - protected $autop_keys = array( + protected $action = 'import_posts_process'; + + protected static $type = 'post'; + + protected static $autop_keys = array( 'post_content', 'post_excerpt', ); - protected function setup_import_data( $objects ) { - - // classify_atts_by_lang and check is_wpml_import - foreach( $objects as $object_i => $object_raw_data ) { + protected static function setup_import_data( $object_raw_data ) { - // clean object_raw_data from not allowed fields - foreach( array( - 'ID', - 'guid', - ) as $not_allowed ) { - if ( array_key_exists( $not_allowed, $object_raw_data ) ) { - unset( $object_raw_data[$not_allowed] ); - } + // clean object_raw_data from not allowed fields + foreach( array( + 'ID', + 'guid', + ) as $not_allowed ) { + if ( array_key_exists( $not_allowed, $object_raw_data ) ) { + unset( $object_raw_data[$not_allowed] ); } - - $this->objects[$object_i] = array( - 'is_wpml_import' => $this->is_wpml_import( $object_raw_data ), - 'inserted' => array(), - 'atts' => $this->classify_atts_by_lang( $object_raw_data, array( - 'post_type', - // ??? may be more - ) ), - ); - } + return array( + 'is_wpml_import' => static::is_wpml_import( $object_raw_data ), + 'inserted' => array(), + 'atts' => static::classify_atts_by_lang( $object_raw_data, array( + 'post_type', + // ??? may be more + ) ), + ); + } - protected function classify_atts_by_validy( $object_atts, $use_deferred = false ) { + protected static function classify_atts_by_validy( $object_atts, $use_deferred = false ) { $valid_insert_args = array( 'post_author', @@ -103,7 +101,7 @@ protected function classify_atts_by_validy( $object_atts, $use_deferred = false return $classified_atts; } - protected function fix_insert_args( $atts ) { + protected static function fix_insert_args( $atts ) { foreach( array( 'insert_args', @@ -144,7 +142,7 @@ protected function fix_insert_args( $atts ) { return $atts; } - protected function insert_object_wmpl( $i, $object ) { + protected function insert_object_wmpl( $object ) { if ( class_exists( 'SitePress' ) ) { global $sitepress; @@ -159,7 +157,7 @@ protected function insert_object_wmpl( $i, $object ) { $object_id = wp_insert_post( $atts_by_lang['insert_args'] ); if ( is_wp_error( $object_id ) ) { - $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); + // // $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); continue; } @@ -167,21 +165,21 @@ protected function insert_object_wmpl( $i, $object ) { $original_id = null === $original_id ? $object_id : $original_id; $object['inserted'][$lang] = $object_id; - $this->objects[$i]['inserted'][$lang] = $object_id; + $object['inserted'][$lang] = $object_id; - $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); + // // $this->log->add_entry( "Inserted " . static::$type . " \$id={$object_id}", $object_id . '_in' ); // object_trid of first inserted object $object_trid = null === $object_trid ? $sitepress->get_element_trid( $object_id ) : $object_trid; - do_action( "yaim_{$this->type}_inserted_for_wpml_translation", - $object_id, - $this->objects[$i], - $lang, - $object_trid - ); + // do_action( "yaim_" . static::$type . "_inserted_for_wpml_translation", + // $object_id, + // $object, + // $lang, + // $object_trid + // ); $translation_id = $sitepress->set_element_language_details( $object_id, @@ -190,49 +188,51 @@ protected function insert_object_wmpl( $i, $object ) { $lang ); - $this->log->add_entry( " \$lang={$lang} \$translation_id={$translation_id}", $object_id . '_in' ); - $this->log->add_entry( $original_id === $object_id ? '' : " as translation for \$id={$original_id}", $object_id . '_in' ); + // // $this->log->add_entry( " \$lang={$lang} \$translation_id={$translation_id}", $object_id . '_in' ); + // // $this->log->add_entry( $original_id === $object_id ? '' : " as translation for \$id={$original_id}", $object_id . '_in' ); - do_action( "yaim_{$this->type}_wpml_language_set", - $object_id, - $this->objects[$i], - $lang, - $object_trid, - $translation_id - ); + // do_action( "yaim_" . static::$type . "_wpml_language_set", + // $object_id, + // $object, + // $lang, + // $object_trid, + // $translation_id + // ); } - $this->update_object_deferred( $i, $object ); - $this->update_object_p2p( $i, $object ); + $object = $this->update_object_deferred( $object ); - if ( isset( $object_id ) ) - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id, $this->log ); // ??? needs $i + $object = $this->update_object_p2p( $object ); + // if ( isset( $object_id ) ) + // do_action( "yaim_" . static::$type . "_inserted", $object, $object_id, $this->log ); // ??? needs $i + + return $object; } - protected function insert_object( $i, $object ) { + protected function insert_object( $object ) { $object_id = wp_insert_post( array_merge( utils\Arr::get( $object, 'atts.all.insert_args', array() ), utils\Arr::get( $object, 'atts.all.deferred_insert_args', array() ) ) ); if ( is_wp_error( $object_id ) ) { - $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); + // $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); return; } - $this->objects[$i]['inserted']['all'] = $object_id; + $object['inserted']['all'] = $object_id; - $this->update_object_p2p( $i, $object ); + $this->update_object_p2p( $object ); - $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); + // $this->log->add_entry( "Inserted " . static::$type . " \$id={$object_id}", $object_id . '_in' ); - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id, $this->log ); + do_action( "yaim_" . static::$type . "_inserted", $object, $object_id, $this->log ); } - protected function update_object_deferred( $i, $object ) { + protected function update_object_deferred( $object ) { foreach( $object['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) @@ -261,15 +261,17 @@ protected function update_object_deferred( $i, $object ) { $updated = wp_update_post( $args, true ); if ( is_wp_error( $updated ) ) { - $this->log->add_entry( "ERROR updating {$object_id}: " . $updated->get_error_message() ); + // // $this->log->add_entry( "ERROR updating {$object_id}: " . $updated->get_error_message() ); continue; } - $this->log->add_entry( "Updated {$this->type} \$id={$object_id} \$args=[" . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . "] and wpml should have done the sync", $object_id . '_up' ); + // // $this->log->add_entry( "Updated " . static::$type . " \$id={$object_id} \$args=[" . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . "] and wpml should have done the sync", $object_id . '_up' ); } + + return $object; } - protected function update_object_p2p( $i, $object ) { + protected function update_object_p2p( $object ) { if ( class_exists( 'P2P_Connection_Type_Factory' ) ) { foreach( $object['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) @@ -310,12 +312,12 @@ protected function update_object_p2p( $i, $object ) { } if ( ! $both_sides_are_posttype ) { - $this->log->add_entry( "WARNING updating {$object_id}: currently p2p connections are only supported, if both connection sides represent a posttype" ); + // $this->log->add_entry( "WARNING updating {$object_id}: currently p2p connections are only supported, if both connection sides represent a posttype" ); continue; } if ( ! $object_side ) { - $this->log->add_entry( "ERROR updating {$object_id}: No side of \$connection_type={$ctype_name} represents \$posttype={$object_post_type} " ); + // $this->log->add_entry( "ERROR updating {$object_id}: No side of \$connection_type={$ctype_name} represents \$posttype={$object_post_type} " ); continue; } @@ -340,7 +342,7 @@ protected function update_object_p2p( $i, $object ) { $conn_post_id = utils\Arr::get( get_posts( $get_post_args ), '0', false ); if ( ! $conn_post_id ) { - $this->log->add_entry( "WARNING updating {$object_id}: can't create p2p connection, can't find post with \$slug={$conn_post_slug}" ); + // $this->log->add_entry( "WARNING updating {$object_id}: can't create p2p connection, can't find post with \$slug={$conn_post_slug}" ); continue; } @@ -352,16 +354,17 @@ protected function update_object_p2p( $i, $object ) { ); if ( is_wp_error( $p2p_id ) ) { - $this->log->add_entry( 'ERROR: ' . $p2p_id->get_error_message() ); + // $this->log->add_entry( 'ERROR: ' . $p2p_id->get_error_message() ); continue; } $connected_post_ids[] = $conn_post_id; } - $this->log->add_entry( "Updated {$this->type} \$id={$object_id} p2p connected with post \$ids=[" . implode( ', ', $connected_post_ids ) . "] \$ctype_name={$ctype_name}", $object_id . '_p2p_' . $conn_post_id ); + // $this->log->add_entry( "Updated " . static::$type . " \$id={$object_id} p2p connected with post \$ids=[" . implode( ', ', $connected_post_ids ) . "] \$ctype_name={$ctype_name}", $object_id . '_p2p_' . $conn_post_id ); } } } + return $object; } } \ No newline at end of file diff --git a/src/classes/Settings_Page.php b/src/classes/Settings_Page.php index c0e119a..7746325 100644 --- a/src/classes/Settings_Page.php +++ b/src/classes/Settings_Page.php @@ -15,6 +15,17 @@ class Settings_Page { protected $cmb_id = 'yaim_options'; + protected $import_posts; + protected $import_terms; + + protected $suported_types = array( + 'posts', + // 'terms', + ); + + + protected $log; + protected static $instance = null; public static function get_instance( $args = array() ) { @@ -38,9 +49,21 @@ public function __construct( $args ){ $this->main_class = $args['main_class']; add_action( 'cmb2_admin_init', array( $this, 'options_page_metabox' ) ); add_action( 'cmb2_options-page_process_fields_' . $this->cmb_id, array( $this, 'process_fields' ), 10, 2 ); + add_action( 'init', array( $this, 'init_importers' ) ); + } + + public function init_importers() { + + + foreach( $this->suported_types as $type ) { + $importer = 'import_' . $type; + $importer_class = __NAMESPACE__ . '\Import_' . ucfirst( $type ); + $this->$importer = new $importer_class(); + } } + public function options_page_metabox() { /** @@ -64,7 +87,7 @@ public function options_page_metabox() { // 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty. // 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page. // 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()). - // 'save_button' => esc_html__( 'Save Theme Options', 'yaim' ), // The text for the options-page save button. Defaults to 'Save'. + 'save_button' => esc_html__( 'Start Import', 'yaim' ), // The text for the options-page save button. Defaults to 'Save'. // 'disable_settings_errors' => true, // On settings pages (not options-general.php sub-pages), allows disabling. 'message_cb' => array( $this, 'message_cb' ), // 'tab_group' => '', // Tab-group identifier, enables options page tab navigation. @@ -89,6 +112,7 @@ function render_empty_field( $field_args, $field ) {} public function options_cb( $field ) { $uploads_dir_path = WP_CONTENT_DIR . '/' . call_user_func( array( $this->main_class, 'get_instance' ) )->slug; + $files = glob( $uploads_dir_path . '/*'); $labels = array_map( function( $file ) use ( $uploads_dir_path ) { @@ -101,7 +125,10 @@ public function options_cb( $field ) { // Do some processing just before the fileds are saved public function process_fields( $cmb, $cmb_id ) { - $this->log = new Log(); + + + $admin_message_log = new Admin_Message_Log(); + $file = utils\Arr::get( $cmb->data_to_save, 'file' ); @@ -112,20 +139,24 @@ public function process_fields( $cmb, $cmb_id ) { // ??? validate $file_data - $cmb->data_to_save['log'] = array(); foreach( $file_data as $type => $objects ) { - switch( $type ) { - case 'posts': - $importer = new Import_Posts( $objects, $this->log ); - break; - case 'terms': - $importer = new Import_Terms( $objects, $this->log ); - break; - default: - $this->log->add_entry( "ERROR: import for {$type} not supported" ); + if( ! in_array( $type, $this->suported_types ) ) { + $admin_message_log->add_entry( "ERROR: import for {$type} not supported" ); + continue; } + + $importer = 'import_' . $type; + + $this->$importer->add_logger( $this->log ); + + foreach( $objects as $object_raw_data ) { + $this->$importer->push_to_queue( $object_raw_data ); + } + $this->$importer->save()->dispatch(); + } + $admin_message_log->save(); } /** @@ -154,10 +185,10 @@ public function message_cb( $cmb, $args ) { if ( empty( $args['should_notify'] ) ) return; - $log = Log::get_from_db(); + $admin_message_log = Admin_Message_Log::get_from_db(); - if ( $log['last_log_saved'] ) { - $args['message'] = implode( '
', $log['msgs'] ); + if ( $admin_message_log['last_log_saved'] ) { + $args['message'] = implode( '
', $admin_message_log['msgs'] ); $args['type'] = strpos( $args['message'], 'ERROR' ) !== false ? 'notice-warning' : 'updated'; From 51a5aaa89431366d6c8beda27b5d993a4dfb6bdf Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Wed, 25 Sep 2019 18:38:21 +0545 Subject: [PATCH 08/11] Background processing for posts done and fixed logging --- src/classes/Admin_Message_Log.php | 6 +- src/classes/Import_Base.php | 43 +++++++++---- src/classes/Import_Posts.php | 100 +++++++++++++++++++++++++----- src/classes/Settings_Page.php | 60 +++++++++--------- 4 files changed, 148 insertions(+), 61 deletions(-) diff --git a/src/classes/Admin_Message_Log.php b/src/classes/Admin_Message_Log.php index a244035..07a7045 100644 --- a/src/classes/Admin_Message_Log.php +++ b/src/classes/Admin_Message_Log.php @@ -18,7 +18,7 @@ class Admin_Message_Log { protected $start; public function __construct(){ - $this->start = date( 'Y-m-d H:i:s' ); + $this->start = date('d-M-Y H:i:s T'); update_option( self::$option_key . '_start', $this->start ); $this->add_entries( array( $this->start, '' ) ); @@ -59,10 +59,6 @@ public static function get_from_db(){ } public function save(){ - $this->add_entries( array( - date( 'Y-m-d H:i:s' ) . ' Save log to db $option_key=' . self::$option_key, - '', - ) ); update_option( self::$option_key, $this->msgs ); } diff --git a/src/classes/Import_Base.php b/src/classes/Import_Base.php index 53f1d17..f945627 100644 --- a/src/classes/Import_Base.php +++ b/src/classes/Import_Base.php @@ -17,22 +17,40 @@ abstract class Import_Base extends \WP_Background_Process { protected static $autop_keys = array(); - protected $log; + protected $uploads_dir_path; // public function __construct(){ // parent::__construct(); // } - public function add_logger( $log ) { - $this->log = $log; + protected static function log( $msg = '' ) { + $path = WP_CONTENT_DIR . '/yaml-importer/import.log'; + error_log( + sprintf( '[%s] %s %s', date('d-M-Y H:i:s T'), $msg, PHP_EOL ), + 3, + $path + ); } protected function task( $object_raw_data ) { + static::log( '' ); + static::log( implode( ' ', array( + 'Start' . "\t", + static::$type, + 'import', + ) ) ); + $object = static::setup_import_data( $object_raw_data ); $object = static::prepare_import_data( $object ); + static::log( implode( ' ', array( + 'Prepared' . "\t", + static::$type, + 'data', + ) ) ); + if ( class_exists( 'SitePress' ) ) { $current_lang = apply_filters( 'wpml_current_language', null ); do_action( 'wpml_switch_language', apply_filters( 'wpml_default_language', null ) ); @@ -47,16 +65,19 @@ protected function task( $object_raw_data ) { } } else { - // $this->log->add_entry( 'ERROR: ' . $object['atts']->get_error_message() ); + static::log( implode( ' ', array( + 'ERROR', + $object['atts']->get_error_message() + ) ) ); } - // $this->log->add_entry( 'done' ); - // $this->log->save(); - if ( class_exists( 'SitePress' ) ) { do_action( 'wpml_switch_language', $current_lang, null ); } + static::log( 'Done' ); + static::log( '' ); + return false; } @@ -129,7 +150,7 @@ protected static function classify_atts_by_lang( $object_raw_data, $not_translat $is_wpml_attr = static::is_wpml_attr( $key, array_keys( $attr ) ); if ( is_wp_error( $is_wpml_attr ) ) { - // $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); + static::log( 'ERROR: ' . $is_wpml_attr->get_error_message() ); continue; } @@ -143,7 +164,7 @@ protected static function classify_atts_by_lang( $object_raw_data, $not_translat $atts[$lang][$key] = static::maybe_autop( $key, $val ); } } else { - // $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); + static::log( "ERROR: \$lang={$lang} not acive" ); } } } else { @@ -153,7 +174,7 @@ protected static function classify_atts_by_lang( $object_raw_data, $not_translat $is_wpml_attr = static::is_wpml_attr( $n_key, array_keys( $n_attr ) ); if ( is_wp_error( $is_wpml_attr ) ) { - // $this->log->add_entry( 'ERROR: ' . $is_wpml_attr->get_error_message() ); + static::log( 'ERROR: ' . $is_wpml_attr->get_error_message() ); continue; } @@ -163,7 +184,7 @@ protected static function classify_atts_by_lang( $object_raw_data, $not_translat if ( in_array( $lang, static::get_active_langs() ) ) { $atts[$lang][$key][$n_key] = static::maybe_autop( $n_key, $val ); } else { - // $this->log->add_entry( "ERROR: \$lang={$lang} not acive" ); + static::log( "ERROR: \$lang={$lang} not acive" ); } } } else { // is normal field diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index 72e3e6b..7a2d965 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -14,6 +14,7 @@ class Import_Posts extends Import_Base { protected $action = 'import_posts_process'; + protected static $type = 'post'; protected static $autop_keys = array( @@ -21,6 +22,8 @@ class Import_Posts extends Import_Base { 'post_excerpt', ); + + protected static function setup_import_data( $object_raw_data ) { // clean object_raw_data from not allowed fields @@ -157,17 +160,25 @@ protected function insert_object_wmpl( $object ) { $object_id = wp_insert_post( $atts_by_lang['insert_args'] ); if ( is_wp_error( $object_id ) ) { - // // $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + $object_id->get_error_message() + ) ) ); continue; } // original_id of first inserted object $original_id = null === $original_id ? $object_id : $original_id; - $object['inserted'][$lang] = $object_id; $object['inserted'][$lang] = $object_id; - // // $this->log->add_entry( "Inserted " . static::$type . " \$id={$object_id}", $object_id . '_in' ); + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $object_id, + ) ) ); // object_trid of first inserted object $object_trid = null === $object_trid @@ -188,8 +199,14 @@ protected function insert_object_wmpl( $object ) { $lang ); - // // $this->log->add_entry( " \$lang={$lang} \$translation_id={$translation_id}", $object_id . '_in' ); - // // $this->log->add_entry( $original_id === $object_id ? '' : " as translation for \$id={$original_id}", $object_id . '_in' ); + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $object_id, + '$lang=' . $lang, + '$translation_id=' . $translation_id, + $original_id === $object_id ? '' : 'as translation for $id=' . $original_id, + ) ) ); // do_action( "yaim_" . static::$type . "_wpml_language_set", // $object_id, @@ -207,7 +224,7 @@ protected function insert_object_wmpl( $object ) { $object = $this->update_object_p2p( $object ); // if ( isset( $object_id ) ) - // do_action( "yaim_" . static::$type . "_inserted", $object, $object_id, $this->log ); // ??? needs $i + // do_action( "yaim_" . static::$type . "_inserted", $object, $object_id ); // ??? needs $i return $object; } @@ -219,7 +236,12 @@ protected function insert_object( $object ) { ) ); if ( is_wp_error( $object_id ) ) { - // $this->log->add_entry( 'ERROR: ' . $object_id->get_error_message() ); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + $object_id->get_error_message() + ) ) ); return; } @@ -227,9 +249,13 @@ protected function insert_object( $object ) { $this->update_object_p2p( $object ); - // $this->log->add_entry( "Inserted " . static::$type . " \$id={$object_id}", $object_id . '_in' ); + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $object_id, + ) ) ); - do_action( "yaim_" . static::$type . "_inserted", $object, $object_id, $this->log ); + // do_action( "yaim_" . static::$type . "_inserted", $object, $object_id ); } protected function update_object_deferred( $object ) { @@ -261,11 +287,25 @@ protected function update_object_deferred( $object ) { $updated = wp_update_post( $args, true ); if ( is_wp_error( $updated ) ) { - // // $this->log->add_entry( "ERROR updating {$object_id}: " . $updated->get_error_message() ); + static::log( implode( ' ', array( + 'WARNING' . "\t", + 'updating', + '$id=' . $object_id, + static::$type, + $updated->get_error_message(), + ) ) ); continue; } - // // $this->log->add_entry( "Updated " . static::$type . " \$id={$object_id} \$args=[" . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . "] and wpml should have done the sync", $object_id . '_up' ); + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $object_id, + '$args=[' . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . ']', + 'and wpml should have done the sync', + ) ) ); + + } return $object; @@ -312,12 +352,22 @@ protected function update_object_p2p( $object ) { } if ( ! $both_sides_are_posttype ) { - // $this->log->add_entry( "WARNING updating {$object_id}: currently p2p connections are only supported, if both connection sides represent a posttype" ); + static::log( implode( ' ', array( + 'WARNING' . "\t", + 'updating', + static::$type, + 'Currently p2p connections are only supported, if both connection sides represent a posttype', + ) ) ); continue; } if ( ! $object_side ) { - // $this->log->add_entry( "ERROR updating {$object_id}: No side of \$connection_type={$ctype_name} represents \$posttype={$object_post_type} " ); + static::log( implode( ' ', array( + 'WARNING' . "\t", + 'updating', + static::$type, + 'No side of $connection_type=' . $ctype_name . ' represents $posttype=' . $object_post_type, + ) ) ); continue; } @@ -342,7 +392,13 @@ protected function update_object_p2p( $object ) { $conn_post_id = utils\Arr::get( get_posts( $get_post_args ), '0', false ); if ( ! $conn_post_id ) { - // $this->log->add_entry( "WARNING updating {$object_id}: can't create p2p connection, can't find post with \$slug={$conn_post_slug}" ); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'updating', + static::$type, + '$id=' . $object_id, + 'can not create p2p connection, can not find post with $slug=' . $conn_post_slug, + ) ) ); continue; } @@ -354,13 +410,25 @@ protected function update_object_p2p( $object ) { ); if ( is_wp_error( $p2p_id ) ) { - // $this->log->add_entry( 'ERROR: ' . $p2p_id->get_error_message() ); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'creating p2p connection', + static::$type, + '$id=' . $object_id, + $object_id->get_error_message() + ) ) ); continue; } $connected_post_ids[] = $conn_post_id; } - // $this->log->add_entry( "Updated " . static::$type . " \$id={$object_id} p2p connected with post \$ids=[" . implode( ', ', $connected_post_ids ) . "] \$ctype_name={$ctype_name}", $object_id . '_p2p_' . $conn_post_id ); + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $object_id, + 'p2p connected with post $ids=[' . implode( ', ', $connected_post_ids ) . ']', + '$ctype_name=' . $ctype_name, + ) ) ); } } } diff --git a/src/classes/Settings_Page.php b/src/classes/Settings_Page.php index 7746325..82fb1e0 100644 --- a/src/classes/Settings_Page.php +++ b/src/classes/Settings_Page.php @@ -13,6 +13,8 @@ class Settings_Page { protected $main_class; + protected $uploads_dir_path; + protected $cmb_id = 'yaim_options'; protected $import_posts; @@ -28,33 +30,32 @@ class Settings_Page { protected static $instance = null; - public static function get_instance( $args = array() ) { + public static function get_instance() { if ( null === self::$instance ) { - $required_args = array( - 'main_class', - ); - foreach( $required_args as $required_arg ) { - if ( ! array_key_exists( $required_arg, $args ) || empty( $args[$required_arg] ) ) { - error_log( __FILE__ ); - return new \WP_Error( 'missing_arg', sprintf( __( 'Argument "%s" missing', 'yaim' ), $required_arg ) ); - } - } - self::$instance = new self( $args ); + // $required_args = array( + // 'main_class', + // ); + // foreach( $required_args as $required_arg ) { + // if ( ! array_key_exists( $required_arg, $args ) || empty( $args[$required_arg] ) ) { + // error_log( __FILE__ ); + // return new \WP_Error( 'missing_arg', sprintf( __( 'Argument "%s" missing', 'yaim' ), $required_arg ) ); + // } + // } + self::$instance = new self(); } return self::$instance; } - public function __construct( $args ){ - $this->main_class = $args['main_class']; + public function __construct(){ + // $this->main_class = $args['main_class']; + // $this->uploads_dir_path = WP_CONTENT_DIR . '/' . call_user_func( array( $this->main_class, 'get_instance' ) )->slug; add_action( 'cmb2_admin_init', array( $this, 'options_page_metabox' ) ); add_action( 'cmb2_options-page_process_fields_' . $this->cmb_id, array( $this, 'process_fields' ), 10, 2 ); add_action( 'init', array( $this, 'init_importers' ) ); } public function init_importers() { - - foreach( $this->suported_types as $type ) { $importer = 'import_' . $type; $importer_class = __NAMESPACE__ . '\Import_' . ucfirst( $type ); @@ -111,14 +112,11 @@ public function options_page_metabox() { function render_empty_field( $field_args, $field ) {} public function options_cb( $field ) { - $uploads_dir_path = WP_CONTENT_DIR . '/' . call_user_func( array( $this->main_class, 'get_instance' ) )->slug; - - $files = glob( $uploads_dir_path . '/*'); - - $labels = array_map( function( $file ) use ( $uploads_dir_path ) { - return str_replace( $uploads_dir_path . '/', '', $file ); + $path = WP_CONTENT_DIR . '/yaml-importer/'; + $files = glob( $path .'*.yaml'); + $labels = array_map( function( $file ) use ( $path ) { + return str_replace( $path, '', $file ); }, $files ); - return array_combine( $files, $labels ); } @@ -126,9 +124,9 @@ public function options_cb( $field ) { // Do some processing just before the fileds are saved public function process_fields( $cmb, $cmb_id ) { - $admin_message_log = new Admin_Message_Log(); + $admin_message_log->add_entry( 'Start Import' ); $file = utils\Arr::get( $cmb->data_to_save, 'file' ); @@ -139,7 +137,7 @@ public function process_fields( $cmb, $cmb_id ) { // ??? validate $file_data - foreach( $file_data as $type => $objects ) { + foreach( $file_data as $type => $items ) { if( ! in_array( $type, $this->suported_types ) ) { $admin_message_log->add_entry( "ERROR: import for {$type} not supported" ); continue; @@ -147,15 +145,17 @@ public function process_fields( $cmb, $cmb_id ) { $importer = 'import_' . $type; - $this->$importer->add_logger( $this->log ); - - foreach( $objects as $object_raw_data ) { - $this->$importer->push_to_queue( $object_raw_data ); + foreach( $items as $item_raw_data ) { + $this->$importer->push_to_queue( $item_raw_data ); } $this->$importer->save()->dispatch(); + $admin_message_log->add_entry( 'All ' . count( $items ) . ' ' . $type . ' queued for import' ); } + $log_path = WP_CONTENT_DIR . '/yaml-importer/import.log'; + $admin_message_log->add_entry( 'Start import in background, check the import.log ' . $log_path ); + $admin_message_log->save(); } @@ -187,13 +187,15 @@ public function message_cb( $cmb, $args ) { $admin_message_log = Admin_Message_Log::get_from_db(); + $log_path = WP_CONTENT_DIR . '/yaml-importer/import.log'; + if ( $admin_message_log['last_log_saved'] ) { $args['message'] = implode( '
', $admin_message_log['msgs'] ); $args['type'] = strpos( $args['message'], 'ERROR' ) !== false ? 'notice-warning' : 'updated'; } else { - $args['message'] = 'Something went wrong and no log saved'; + $args['message'] = 'Something went wrong'; $args['type'] = 'error'; } From ec33d1454f4250a242165122ab0263c9cb0745ea Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Wed, 25 Sep 2019 19:11:57 +0545 Subject: [PATCH 09/11] Background processing for terms done --- src/classes/Import_Posts.php | 16 ++-- src/classes/Import_Terms.php | 135 +++++++++++++++++++++------------- src/classes/Settings_Page.php | 13 +++- 3 files changed, 99 insertions(+), 65 deletions(-) diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index 7a2d965..b7b811f 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -11,21 +11,16 @@ class Import_Posts extends Import_Base { - - protected $action = 'import_posts_process'; - + protected $action = 'import_post'; protected static $type = 'post'; - protected static $autop_keys = array( + protected static $autop_keys = array( 'post_content', 'post_excerpt', ); - - protected static function setup_import_data( $object_raw_data ) { - // clean object_raw_data from not allowed fields foreach( array( 'ID', @@ -44,7 +39,6 @@ protected static function setup_import_data( $object_raw_data ) { // ??? may be more ) ), ); - } protected static function classify_atts_by_validy( $object_atts, $use_deferred = false ) { @@ -164,7 +158,7 @@ protected function insert_object_wmpl( $object ) { 'ERROR' . "\t", 'inserting', static::$type, - $object_id->get_error_message() + 'Message: ' . $object_id->get_error_message() ) ) ); continue; } @@ -240,7 +234,7 @@ protected function insert_object( $object ) { 'ERROR' . "\t", 'inserting', static::$type, - $object_id->get_error_message() + 'Message: ' . $object_id->get_error_message() ) ) ); return; } @@ -415,7 +409,7 @@ protected function update_object_p2p( $object ) { 'creating p2p connection', static::$type, '$id=' . $object_id, - $object_id->get_error_message() + 'Message: ' . $object_id->get_error_message() ) ) ); continue; } diff --git a/src/classes/Import_Terms.php b/src/classes/Import_Terms.php index 48202a9..4b0cc7c 100644 --- a/src/classes/Import_Terms.php +++ b/src/classes/Import_Terms.php @@ -11,30 +11,25 @@ class Import_Terms extends Import_Base { - protected $type = 'term'; + protected $action = 'import_term'; - protected $autop_keys = array( + protected static $type = 'term'; + + protected static $autop_keys = array( 'description', ); - protected function setup_import_data( $objects ) { - - // classify_atts_by_lang and check is_wpml_import - foreach( $objects as $object_i => $object_raw_data ) { - - $this->objects[$object_i] = array( - 'is_wpml_import' => $this->is_wpml_import( $object_raw_data ), - 'inserted' => array(), - 'atts' => $this->classify_atts_by_lang( $object_raw_data, array( - 'taxonomy', - ) ), - ); - - } - + protected static function setup_import_data( $object_raw_data ) { + return array( + 'is_wpml_import' => static::is_wpml_import( $object_raw_data ), + 'inserted' => array(), + 'atts' => static::classify_atts_by_lang( $object_raw_data, array( + 'taxonomy', + ) ), + ); } - protected function classify_atts_by_validy( $object_atts, $use_deferred = false ) { + protected static function classify_atts_by_validy( $object_atts, $use_deferred = false ) { $valid_insert_args = array( 'name', @@ -60,7 +55,7 @@ protected function classify_atts_by_validy( $object_atts, $use_deferred = false return $classified_atts; } - protected function fix_insert_args( $atts ) { + protected static function fix_insert_args( $atts ) { foreach( array( 'parent', @@ -81,7 +76,7 @@ protected function fix_insert_args( $atts ) { return $atts; } - protected function insert_object_wmpl( $i, $object ) { + protected function insert_object_wmpl( $object ) { if ( class_exists( 'SitePress' ) ) { global $sitepress; @@ -100,7 +95,12 @@ protected function insert_object_wmpl( $i, $object ) { $term_tax_ids = wp_insert_term( $name, $taxonomy, $args ); if ( is_wp_error( $term_tax_ids ) ) { - $this->log->add_entry( 'ERROR: ' . $term_tax_ids->get_error_message() ); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + 'Message: ' . $term_tax_ids->get_error_message() + ) ) ); continue; } @@ -110,21 +110,24 @@ protected function insert_object_wmpl( $i, $object ) { $original_id = null === $original_id ? $object_id : $original_id; $object['inserted'][$lang] = $object_id; - $this->objects[$i]['inserted'][$lang] = $object_id; - $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $object_id, + ) ) ); // object_trid of first inserted object $object_trid = null === $object_trid ? apply_filters( 'wpml_element_trid', NULL, $object_id, 'tax_' . $taxonomy ) : $object_trid; - do_action( "yaim_{$this->type}_inserted_for_wpml_translation", - $object_id, - $this->objects[$i], - $lang, - $object_trid - ); + // do_action( "yaim_{$this->type}_inserted_for_wpml_translation", + // $object_id, + // $this->objects[$i], + // $lang, + // $object_trid + // ); $translation_id = $sitepress->set_element_language_details( $object_id, @@ -133,16 +136,22 @@ protected function insert_object_wmpl( $i, $object ) { $lang ); - $this->log->add_entry( " \$lang={$lang} \$translation_id={$translation_id}", $object_id . '_in' ); - $this->log->add_entry( $original_id === $object_id ? '' : " as translation for \$id={$original_id}", $object_id . '_in' ); - - do_action( "yaim_{$this->type}_wpml_language_set", - $object_id, - $this->objects[$i], - $lang, - $object_trid, - $translation_id - ); + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $object_id, + '$lang=' . $lang, + '$translation_id=' . $translation_id, + $original_id === $object_id ? '' : 'as translation for $id=' . $original_id, + ) ) ); + + // do_action( "yaim_{$this->type}_wpml_language_set", + // $object_id, + // $this->objects[$i], + // $lang, + // $object_trid, + // $translation_id + // ); } // handle meta_input @@ -162,25 +171,39 @@ protected function insert_object_wmpl( $i, $object ) { foreach( $meta_input as $meta_key => $meta_val ) { $updated = update_term_meta( $object_id, $meta_key, $meta_val ); - if ( is_numeric( $updated ) && $updated == ( int ) $updated ) { - $this->log->add_entry( "Updated {$this->type} \$id={$object_id} added \$meta_key={$meta_key} \$meta_id={$updated}", $object_id . '_' . $meta_key ); - + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $object_id, + 'added $meta_key=' . $meta_key . ' $meta_id ' . $updated, + ) ) ); } elseif ( $updated ) { - $this->log->add_entry( "Updated {$this->type} \$id={$object_id} updated \$meta_key={$meta_key}", $object_id . '_' . $meta_key ); + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $object_id, + 'updated $meta_key=' . $meta_key, + ) ) ); } else { - $this->log->add_entry( "ERROR updating {$object_id}: couldn't add \$meta_key={$meta_key}", $object_id . '_' . $meta_key ); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'updating', + static::$type, + '$id=' . $object_id, + 'could not add $meta_key=' . $meta_key, + ) ) ); continue; } } } - if ( isset( $object_id ) ) - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + // if ( isset( $object_id ) ) + // do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); } - protected function insert_object( $i, $object ) { + protected function insert_object( $object ) { $args = utils\Arr::get( $object, 'atts.all.insert_args', array() ); $name = array_splice( $args, array_search( 'name', array_keys( $args ) ), 1 )['name']; $taxonomy = array_splice( $args, array_search( 'taxonomy', array_keys( $args ) ), 1 )['taxonomy']; @@ -188,15 +211,25 @@ protected function insert_object( $i, $object ) { $term_tax_ids = wp_insert_term( $name, $taxonomy, $args ); if ( is_wp_error( $term_tax_ids ) ) { - $this->log->add_entry( 'ERROR: ' . $term_tax_ids->get_error_message(); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + 'Message: ' . $term_tax_ids->get_error_message() + ) ) ); return; } $object_id = $term_tax_ids['term_id']; - $this->objects[$i]['inserted']['all'] = $object_id; + $object['inserted']['all'] = $object_id; + + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $object_id, + ) ) ); - $this->log->add_entry( "Inserted {$this->type} \$id={$object_id}", $object_id . '_in' ); - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], null ); + // do_action( "yaim_{$this->type}_inserted", $this->objects[$i], null ); } } \ No newline at end of file diff --git a/src/classes/Settings_Page.php b/src/classes/Settings_Page.php index 82fb1e0..4d3337a 100644 --- a/src/classes/Settings_Page.php +++ b/src/classes/Settings_Page.php @@ -22,7 +22,7 @@ class Settings_Page { protected $suported_types = array( 'posts', - // 'terms', + 'terms', ); @@ -137,9 +137,10 @@ public function process_fields( $cmb, $cmb_id ) { // ??? validate $file_data + $types_queued = array(); foreach( $file_data as $type => $items ) { if( ! in_array( $type, $this->suported_types ) ) { - $admin_message_log->add_entry( "ERROR: import for {$type} not supported" ); + $admin_message_log->add_entry( 'ERROR: Import for ' . $type . ' not supported' ); continue; } @@ -149,12 +150,18 @@ public function process_fields( $cmb, $cmb_id ) { $this->$importer->push_to_queue( $item_raw_data ); } $this->$importer->save()->dispatch(); + $types_queued[] = $type; $admin_message_log->add_entry( 'All ' . count( $items ) . ' ' . $type . ' queued for import' ); } $log_path = WP_CONTENT_DIR . '/yaml-importer/import.log'; - $admin_message_log->add_entry( 'Start import in background, check the import.log ' . $log_path ); + + if ( count( $types_queued ) ) { + $admin_message_log->add_entry( 'Start import ' . implode( ', ', $types_queued ) . ' in background, check ' . $log_path ); + } else { + $admin_message_log->add_entry( 'Nothing imported' ); + } $admin_message_log->save(); } From 1207ed1693d642153638302585b8cf3df2214d82 Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Wed, 25 Sep 2019 19:30:10 +0545 Subject: [PATCH 10/11] Rename object -> item --- .wde_nextRelease.json | 4 +- src/classes/Import_Base.php | 68 ++++++--------- src/classes/Import_Posts.php | 150 +++++++++++++++++----------------- src/classes/Import_Terms.php | 84 +++++++++---------- src/classes/Settings_Page.php | 17 ---- src/classes/Yaim.php | 4 +- 6 files changed, 146 insertions(+), 181 deletions(-) diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 780bc4b..3201217 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -10,7 +10,7 @@ }, { "type": "added", - "message": "Use `wpautop`for certain fields. Fields are filtered by \"yaim_{$this->type}_autop_keys\" filter" + "message": "Use `wpautop`for certain fields. Fields are filtered by \"yaim_{$type}_autop_keys\" filter" }, { "type": "added", @@ -22,7 +22,7 @@ }, { "type": "added", - "message": "a5hleyrich/wp-background-processing" + "message": "Use a5hleyrich/wp-background-processing and process all imports in background" } ] } \ No newline at end of file diff --git a/src/classes/Import_Base.php b/src/classes/Import_Base.php index f945627..159be7c 100644 --- a/src/classes/Import_Base.php +++ b/src/classes/Import_Base.php @@ -11,7 +11,7 @@ abstract class Import_Base extends \WP_Background_Process { - protected $objects = array(); + protected $items = array(); protected static $type = ''; // eg. post, term @@ -32,7 +32,7 @@ protected static function log( $msg = '' ) { ); } - protected function task( $object_raw_data ) { + protected function task( $item_raw_data ) { static::log( '' ); static::log( implode( ' ', array( @@ -41,9 +41,9 @@ protected function task( $object_raw_data ) { 'import', ) ) ); - $object = static::setup_import_data( $object_raw_data ); + $item = static::setup_import_data( $item_raw_data ); - $object = static::prepare_import_data( $object ); + $item = static::prepare_import_data( $item ); static::log( implode( ' ', array( 'Prepared' . "\t", @@ -56,18 +56,18 @@ protected function task( $object_raw_data ) { do_action( 'wpml_switch_language', apply_filters( 'wpml_default_language', null ) ); } - if ( ! is_wp_error( $object['atts'] ) ) { + if ( ! is_wp_error( $item['atts'] ) ) { - if ( $object['is_wpml_import'] ) { - $object = $this->insert_object_wmpl( $object ); + if ( $item['is_wpml_import'] ) { + $item = $this->insert_item_wmpl( $item ); } else { - $object = $this->insert_object( $object ); + $item = $this->insert_item( $item ); } } else { static::log( implode( ' ', array( 'ERROR', - $object['atts']->get_error_message() + $item['atts']->get_error_message() ) ) ); } @@ -81,52 +81,52 @@ protected function task( $object_raw_data ) { return false; } - abstract protected static function setup_import_data( $object ); + abstract protected static function setup_import_data( $item ); // classify_atts_by_validy for wp_insert_post wp_insert_term function // and fix_insert_args - protected static function prepare_import_data( $object ) { - if ( $object['is_wpml_import'] ) { - foreach( $object['atts'] as $lang => $atts ) { + protected static function prepare_import_data( $item ) { + if ( $item['is_wpml_import'] ) { + foreach( $item['atts'] as $lang => $atts ) { if ( 'all' === $lang ) continue; // merge atts recursive for all into current atts_by_lang $atts = array_merge_recursive( $atts, - utils\Arr::get( $object, 'atts.all', array() ) + utils\Arr::get( $item, 'atts.all', array() ) ); - $atts = static::classify_atts_by_validy( $atts, $object['is_wpml_import'] ); + $atts = static::classify_atts_by_validy( $atts, $item['is_wpml_import'] ); $atts = static::fix_insert_args( $atts ); - $object['atts'][$lang] = apply_filters( "yaim_" . static::$type . "_atts", $atts, $lang, $object ); + $item['atts'][$lang] = apply_filters( "yaim_" . static::$type . "_atts", $atts, $lang, $item ); } } else { - $atts = static::classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); + $atts = static::classify_atts_by_validy( utils\Arr::get( $item, 'atts.all', array() ), $item['is_wpml_import'] ); $atts = static::fix_insert_args( $atts ); - $object['atts']['all'] = apply_filters( "yaim_" . static::$type . "_atts", $atts, 'all', $object ); + $item['atts']['all'] = apply_filters( "yaim_" . static::$type . "_atts", $atts, 'all', $item ); } - return $object; + return $item; } - abstract protected static function classify_atts_by_validy( $object_atts, $use_deferred = false ); + abstract protected static function classify_atts_by_validy( $item_atts, $use_deferred = false ); protected static function fix_insert_args( $atts ) { return $atts; } - abstract protected function insert_object( $object ); + abstract protected function insert_item( $item ); - abstract protected function insert_object_wmpl( $object ); + abstract protected function insert_item_wmpl( $item ); - protected static function is_wpml_import( $object_raw_data ) { + protected static function is_wpml_import( $item_raw_data ) { $is_wpml_import = false; - foreach( $object_raw_data as $key => $attr ) { + foreach( $item_raw_data as $key => $attr ) { if ( $is_wpml_import ) break; $is_wpml_import = is_array( $attr ) @@ -135,7 +135,7 @@ protected static function is_wpml_import( $object_raw_data ) { return $is_wpml_import; } - protected static function classify_atts_by_lang( $object_raw_data, $not_translatable = array() ) { + protected static function classify_atts_by_lang( $item_raw_data, $not_translatable = array() ) { $atts = array( 'all' => array() ); @@ -145,7 +145,7 @@ protected static function classify_atts_by_lang( $object_raw_data, $not_translat if ( null !== $default_lang ) $atts[$default_lang] = array(); - foreach( $object_raw_data as $key => $attr ) { + foreach( $item_raw_data as $key => $attr ) { if ( is_array( $attr ) ) { $is_wpml_attr = static::is_wpml_attr( $key, array_keys( $attr ) ); @@ -229,20 +229,4 @@ protected static function get_active_langs() { : array(); } - // /** - // * Public getter method for retrieving protected/private variables - // * @param string $field Field to retrieve - // * @return mixed Field value or exception is thrown - // */ - // public function __get( $field ) { - // if ( in_array( $field, array( - // 'objects', - // 'type', - // 'log', - // ), true ) ) { - // return $this->{$field}; - // } - // throw new Exception( 'Invalid property: ' . $field ); - // } - } \ No newline at end of file diff --git a/src/classes/Import_Posts.php b/src/classes/Import_Posts.php index b7b811f..d371539 100644 --- a/src/classes/Import_Posts.php +++ b/src/classes/Import_Posts.php @@ -20,28 +20,28 @@ class Import_Posts extends Import_Base { 'post_excerpt', ); - protected static function setup_import_data( $object_raw_data ) { - // clean object_raw_data from not allowed fields + protected static function setup_import_data( $item_raw_data ) { + // clean item_raw_data from not allowed fields foreach( array( 'ID', 'guid', ) as $not_allowed ) { - if ( array_key_exists( $not_allowed, $object_raw_data ) ) { - unset( $object_raw_data[$not_allowed] ); + if ( array_key_exists( $not_allowed, $item_raw_data ) ) { + unset( $item_raw_data[$not_allowed] ); } } return array( - 'is_wpml_import' => static::is_wpml_import( $object_raw_data ), + 'is_wpml_import' => static::is_wpml_import( $item_raw_data ), 'inserted' => array(), - 'atts' => static::classify_atts_by_lang( $object_raw_data, array( + 'atts' => static::classify_atts_by_lang( $item_raw_data, array( 'post_type', // ??? may be more ) ), ); } - protected static function classify_atts_by_validy( $object_atts, $use_deferred = false ) { + protected static function classify_atts_by_validy( $item_atts, $use_deferred = false ) { $valid_insert_args = array( 'post_author', @@ -85,7 +85,7 @@ protected static function classify_atts_by_validy( $object_atts, $use_deferred = 'custom_args' => array(), ); - foreach( $object_atts as $key => $val ) { + foreach( $item_atts as $key => $val ) { if ( $use_deferred && in_array( $key, $deferred_insert_args ) ) { $classified_atts['deferred_insert_args'][$key] = $val; } elseif ( in_array( $key, $valid_insert_args ) ) { @@ -139,122 +139,122 @@ protected static function fix_insert_args( $atts ) { return $atts; } - protected function insert_object_wmpl( $object ) { + protected function insert_item_wmpl( $item ) { if ( class_exists( 'SitePress' ) ) { global $sitepress; } - $object_trid = null; + $item_trid = null; $original_id = null; - foreach( $object['atts'] as $lang => $atts_by_lang ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; - $object_id = wp_insert_post( $atts_by_lang['insert_args'] ); + $item_id = wp_insert_post( $atts_by_lang['insert_args'] ); - if ( is_wp_error( $object_id ) ) { + if ( is_wp_error( $item_id ) ) { static::log( implode( ' ', array( 'ERROR' . "\t", 'inserting', static::$type, - 'Message: ' . $object_id->get_error_message() + 'Message: ' . $item_id->get_error_message() ) ) ); continue; } - // original_id of first inserted object - $original_id = null === $original_id ? $object_id : $original_id; + // original_id of first inserted item + $original_id = null === $original_id ? $item_id : $original_id; - $object['inserted'][$lang] = $object_id; + $item['inserted'][$lang] = $item_id; static::log( implode( ' ', array( 'Inserted' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, ) ) ); - // object_trid of first inserted object - $object_trid = null === $object_trid - ? $sitepress->get_element_trid( $object_id ) - : $object_trid; + // item_trid of first inserted item + $item_trid = null === $item_trid + ? $sitepress->get_element_trid( $item_id ) + : $item_trid; // do_action( "yaim_" . static::$type . "_inserted_for_wpml_translation", - // $object_id, - // $object, + // $item_id, + // $item, // $lang, - // $object_trid + // $item_trid // ); $translation_id = $sitepress->set_element_language_details( - $object_id, - 'post_' . get_post_type( $object_id ), - $object_trid, + $item_id, + 'post_' . get_post_type( $item_id ), + $item_trid, $lang ); static::log( implode( ' ', array( 'Updated' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, '$lang=' . $lang, '$translation_id=' . $translation_id, - $original_id === $object_id ? '' : 'as translation for $id=' . $original_id, + $original_id === $item_id ? '' : 'as translation for $id=' . $original_id, ) ) ); // do_action( "yaim_" . static::$type . "_wpml_language_set", - // $object_id, - // $object, + // $item_id, + // $item, // $lang, - // $object_trid, + // $item_trid, // $translation_id // ); } - $object = $this->update_object_deferred( $object ); + $item = $this->update_item_deferred( $item ); - $object = $this->update_object_p2p( $object ); + $item = $this->update_item_p2p( $item ); - // if ( isset( $object_id ) ) - // do_action( "yaim_" . static::$type . "_inserted", $object, $object_id ); // ??? needs $i + // if ( isset( $item_id ) ) + // do_action( "yaim_" . static::$type . "_inserted", $item, $item_id ); // ??? needs $i - return $object; + return $item; } - protected function insert_object( $object ) { - $object_id = wp_insert_post( array_merge( - utils\Arr::get( $object, 'atts.all.insert_args', array() ), - utils\Arr::get( $object, 'atts.all.deferred_insert_args', array() ) + protected function insert_item( $item ) { + $item_id = wp_insert_post( array_merge( + utils\Arr::get( $item, 'atts.all.insert_args', array() ), + utils\Arr::get( $item, 'atts.all.deferred_insert_args', array() ) ) ); - if ( is_wp_error( $object_id ) ) { + if ( is_wp_error( $item_id ) ) { static::log( implode( ' ', array( 'ERROR' . "\t", 'inserting', static::$type, - 'Message: ' . $object_id->get_error_message() + 'Message: ' . $item_id->get_error_message() ) ) ); return; } - $object['inserted']['all'] = $object_id; + $item['inserted']['all'] = $item_id; - $this->update_object_p2p( $object ); + $this->update_item_p2p( $item ); static::log( implode( ' ', array( 'Inserted' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, ) ) ); - // do_action( "yaim_" . static::$type . "_inserted", $object, $object_id ); + // do_action( "yaim_" . static::$type . "_inserted", $item, $item_id ); } - protected function update_object_deferred( $object ) { + protected function update_item_deferred( $item ) { - foreach( $object['atts'] as $lang => $atts_by_lang ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; @@ -262,15 +262,15 @@ protected function update_object_deferred( $object ) { || empty( $atts_by_lang['deferred_insert_args'] ) ) continue; - $object_id = utils\Arr::get( $object, 'inserted.' . $lang, false ); + $item_id = utils\Arr::get( $item, 'inserted.' . $lang, false ); - if ( ! $object_id ) + if ( ! $item_id ) continue; $args = array_merge( array( - 'ID' => $object_id, - 'post_type' => get_post_type( $object_id ), + 'ID' => $item_id, + 'post_type' => get_post_type( $item_id ), ), $atts_by_lang['deferred_insert_args'] ); @@ -284,7 +284,7 @@ protected function update_object_deferred( $object ) { static::log( implode( ' ', array( 'WARNING' . "\t", 'updating', - '$id=' . $object_id, + '$id=' . $item_id, static::$type, $updated->get_error_message(), ) ) ); @@ -294,7 +294,7 @@ protected function update_object_deferred( $object ) { static::log( implode( ' ', array( 'Updated' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, '$args=[' . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . ']', 'and wpml should have done the sync', ) ) ); @@ -302,12 +302,12 @@ protected function update_object_deferred( $object ) { } - return $object; + return $item; } - protected function update_object_p2p( $object ) { + protected function update_item_p2p( $item ) { if ( class_exists( 'P2P_Connection_Type_Factory' ) ) { - foreach( $object['atts'] as $lang => $atts_by_lang ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; @@ -316,17 +316,17 @@ protected function update_object_p2p( $object ) { if ( ! $p2p ) continue; - $object_id = utils\Arr::get( $object, 'inserted.' . $lang, false ); + $item_id = utils\Arr::get( $item, 'inserted.' . $lang, false ); - if ( ! $object_id ) + if ( ! $item_id ) continue; foreach( $p2p as $ctype_name => $conns ) { $ctype = \P2P_Connection_Type_Factory::get_instance( $ctype_name ); - $object_side = false; - $object_post_type = get_post_type( $object_id ); + $item_side = false; + $item_post_type = get_post_type( $item_id ); $both_sides_are_posttype = true; $conn_post_type = false; foreach( $ctype->side as $direction => $side ) { @@ -334,10 +334,10 @@ protected function update_object_p2p( $object ) { $both_sides_are_posttype = false; continue; } - if ( in_array( $object_post_type, utils\Arr::get( $side->query_vars, 'post_type' ) ) ) { - $object_side = ! $object_side + if ( in_array( $item_post_type, utils\Arr::get( $side->query_vars, 'post_type' ) ) ) { + $item_side = ! $item_side ? $direction - : $object_side; + : $item_side; } else { $conn_post_type = ! $conn_post_type ? utils\Arr::get( $side->query_vars, 'post_type.0' ) @@ -355,12 +355,12 @@ protected function update_object_p2p( $object ) { continue; } - if ( ! $object_side ) { + if ( ! $item_side ) { static::log( implode( ' ', array( 'WARNING' . "\t", 'updating', static::$type, - 'No side of $connection_type=' . $ctype_name . ' represents $posttype=' . $object_post_type, + 'No side of $connection_type=' . $ctype_name . ' represents $posttype=' . $item_post_type, ) ) ); continue; } @@ -390,7 +390,7 @@ protected function update_object_p2p( $object ) { 'ERROR' . "\t", 'updating', static::$type, - '$id=' . $object_id, + '$id=' . $item_id, 'can not create p2p connection, can not find post with $slug=' . $conn_post_slug, ) ) ); continue; @@ -398,8 +398,8 @@ protected function update_object_p2p( $object ) { // create connection $p2p_id = $ctype->connect( - 'from' === $object_side ? $object_id : $conn_post_id, - 'to' === $object_side ? $object_id : $conn_post_id, + 'from' === $item_side ? $item_id : $conn_post_id, + 'to' === $item_side ? $item_id : $conn_post_id, is_array( $conn_v ) ? $conn_v : array() ); @@ -408,8 +408,8 @@ protected function update_object_p2p( $object ) { 'ERROR' . "\t", 'creating p2p connection', static::$type, - '$id=' . $object_id, - 'Message: ' . $object_id->get_error_message() + '$id=' . $item_id, + 'Message: ' . $item_id->get_error_message() ) ) ); continue; } @@ -419,14 +419,14 @@ protected function update_object_p2p( $object ) { static::log( implode( ' ', array( 'Updated' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, 'p2p connected with post $ids=[' . implode( ', ', $connected_post_ids ) . ']', '$ctype_name=' . $ctype_name, ) ) ); } } } - return $object; + return $item; } } \ No newline at end of file diff --git a/src/classes/Import_Terms.php b/src/classes/Import_Terms.php index 4b0cc7c..8743596 100644 --- a/src/classes/Import_Terms.php +++ b/src/classes/Import_Terms.php @@ -19,17 +19,17 @@ class Import_Terms extends Import_Base { 'description', ); - protected static function setup_import_data( $object_raw_data ) { + protected static function setup_import_data( $item_raw_data ) { return array( - 'is_wpml_import' => static::is_wpml_import( $object_raw_data ), + 'is_wpml_import' => static::is_wpml_import( $item_raw_data ), 'inserted' => array(), - 'atts' => static::classify_atts_by_lang( $object_raw_data, array( + 'atts' => static::classify_atts_by_lang( $item_raw_data, array( 'taxonomy', ) ), ); } - protected static function classify_atts_by_validy( $object_atts, $use_deferred = false ) { + protected static function classify_atts_by_validy( $item_atts, $use_deferred = false ) { $valid_insert_args = array( 'name', @@ -44,7 +44,7 @@ protected static function classify_atts_by_validy( $object_atts, $use_deferred = 'custom_args' => array(), ); - foreach( $object_atts as $key => $val ) { + foreach( $item_atts as $key => $val ) { if ( in_array( $key, $valid_insert_args ) ) { $classified_atts['insert_args'][$key] = $val; } else { @@ -76,15 +76,15 @@ protected static function fix_insert_args( $atts ) { return $atts; } - protected function insert_object_wmpl( $object ) { + protected function insert_item_wmpl( $item ) { if ( class_exists( 'SitePress' ) ) { global $sitepress; } - $object_trid = null; + $item_trid = null; $original_id = null; - foreach( $object['atts'] as $lang => $atts_by_lang ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; @@ -104,58 +104,58 @@ protected function insert_object_wmpl( $object ) { continue; } - $object_id = $term_tax_ids['term_id']; + $item_id = $term_tax_ids['term_id']; - // original_id of first inserted object - $original_id = null === $original_id ? $object_id : $original_id; + // original_id of first inserted item + $original_id = null === $original_id ? $item_id : $original_id; - $object['inserted'][$lang] = $object_id; + $item['inserted'][$lang] = $item_id; static::log( implode( ' ', array( 'Inserted' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, ) ) ); - // object_trid of first inserted object - $object_trid = null === $object_trid - ? apply_filters( 'wpml_element_trid', NULL, $object_id, 'tax_' . $taxonomy ) - : $object_trid; + // item_trid of first inserted item + $item_trid = null === $item_trid + ? apply_filters( 'wpml_element_trid', NULL, $item_id, 'tax_' . $taxonomy ) + : $item_trid; // do_action( "yaim_{$this->type}_inserted_for_wpml_translation", - // $object_id, - // $this->objects[$i], + // $item_id, + // $this->items[$i], // $lang, - // $object_trid + // $item_trid // ); $translation_id = $sitepress->set_element_language_details( - $object_id, + $item_id, 'tax_' . $taxonomy, - $object_trid, + $item_trid, $lang ); static::log( implode( ' ', array( 'Updated' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, '$lang=' . $lang, '$translation_id=' . $translation_id, - $original_id === $object_id ? '' : 'as translation for $id=' . $original_id, + $original_id === $item_id ? '' : 'as translation for $id=' . $original_id, ) ) ); // do_action( "yaim_{$this->type}_wpml_language_set", - // $object_id, - // $this->objects[$i], + // $item_id, + // $this->items[$i], // $lang, - // $object_trid, + // $item_trid, // $translation_id // ); } // handle meta_input - foreach( $object['atts'] as $lang => $atts_by_lang ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; @@ -164,25 +164,25 @@ protected function insert_object_wmpl( $object ) { if ( ! $meta_input ) continue; - $object_id = utils\Arr::get( $object, 'inserted.' . $lang, false ); + $item_id = utils\Arr::get( $item, 'inserted.' . $lang, false ); - if ( ! $object_id ) + if ( ! $item_id ) continue; foreach( $meta_input as $meta_key => $meta_val ) { - $updated = update_term_meta( $object_id, $meta_key, $meta_val ); + $updated = update_term_meta( $item_id, $meta_key, $meta_val ); if ( is_numeric( $updated ) && $updated == ( int ) $updated ) { static::log( implode( ' ', array( 'Updated' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, 'added $meta_key=' . $meta_key . ' $meta_id ' . $updated, ) ) ); } elseif ( $updated ) { static::log( implode( ' ', array( 'Updated' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, 'updated $meta_key=' . $meta_key, ) ) ); } else { @@ -190,7 +190,7 @@ protected function insert_object_wmpl( $object ) { 'ERROR' . "\t", 'updating', static::$type, - '$id=' . $object_id, + '$id=' . $item_id, 'could not add $meta_key=' . $meta_key, ) ) ); continue; @@ -198,13 +198,13 @@ protected function insert_object_wmpl( $object ) { } } - // if ( isset( $object_id ) ) - // do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + // if ( isset( $item_id ) ) + // do_action( "yaim_{$this->type}_inserted", $this->items[$i], $item_id ); } - protected function insert_object( $object ) { - $args = utils\Arr::get( $object, 'atts.all.insert_args', array() ); + protected function insert_item( $item ) { + $args = utils\Arr::get( $item, 'atts.all.insert_args', array() ); $name = array_splice( $args, array_search( 'name', array_keys( $args ) ), 1 )['name']; $taxonomy = array_splice( $args, array_search( 'taxonomy', array_keys( $args ) ), 1 )['taxonomy']; @@ -220,16 +220,16 @@ protected function insert_object( $object ) { return; } - $object_id = $term_tax_ids['term_id']; - $object['inserted']['all'] = $object_id; + $item_id = $term_tax_ids['term_id']; + $item['inserted']['all'] = $item_id; static::log( implode( ' ', array( 'Inserted' . "\t", static::$type, - '$id=' . $object_id, + '$id=' . $item_id, ) ) ); - // do_action( "yaim_{$this->type}_inserted", $this->objects[$i], null ); + // do_action( "yaim_{$this->type}_inserted", $this->items[$i], null ); } } \ No newline at end of file diff --git a/src/classes/Settings_Page.php b/src/classes/Settings_Page.php index 4d3337a..d5c8858 100644 --- a/src/classes/Settings_Page.php +++ b/src/classes/Settings_Page.php @@ -25,31 +25,16 @@ class Settings_Page { 'terms', ); - - protected $log; - protected static $instance = null; public static function get_instance() { if ( null === self::$instance ) { - // $required_args = array( - // 'main_class', - // ); - // foreach( $required_args as $required_arg ) { - // if ( ! array_key_exists( $required_arg, $args ) || empty( $args[$required_arg] ) ) { - // error_log( __FILE__ ); - // return new \WP_Error( 'missing_arg', sprintf( __( 'Argument "%s" missing', 'yaim' ), $required_arg ) ); - // } - // } self::$instance = new self(); } return self::$instance; } - public function __construct(){ - // $this->main_class = $args['main_class']; - // $this->uploads_dir_path = WP_CONTENT_DIR . '/' . call_user_func( array( $this->main_class, 'get_instance' ) )->slug; add_action( 'cmb2_admin_init', array( $this, 'options_page_metabox' ) ); add_action( 'cmb2_options-page_process_fields_' . $this->cmb_id, array( $this, 'process_fields' ), 10, 2 ); add_action( 'init', array( $this, 'init_importers' ) ); @@ -61,10 +46,8 @@ public function init_importers() { $importer_class = __NAMESPACE__ . '\Import_' . ucfirst( $type ); $this->$importer = new $importer_class(); } - } - public function options_page_metabox() { /** diff --git a/src/classes/Yaim.php b/src/classes/Yaim.php index 6d5b118..add9af1 100644 --- a/src/classes/Yaim.php +++ b/src/classes/Yaim.php @@ -14,9 +14,7 @@ class Yaim extends wde\Plugin { public function hooks(){ parent::hooks(); - Settings_Page::get_instance( array( - 'main_class' => __CLASS__, - ) ); + Settings_Page::get_instance(); } } \ No newline at end of file From 40f0a5416239914004517d9e97a1e5c9762c6fda Mon Sep 17 00:00:00 2001 From: jhotadhari Date: Wed, 25 Sep 2019 19:35:23 +0545 Subject: [PATCH 11/11] ## 0.1.0 - 2019-09-25 Background Processing ### Added - Handle `meta_input` field for Term imports - Use `wpautop`for certain fields. Fields are filtered by "yaim_{$type}_autop_keys" filter - Handle p2p connections for post import - Use a5hleyrich/wp-background-processing and process all imports in background ### Changed - More stable log ### Fixed - Multilang values for directly nested attributes. eg `meta_input`. Also works if some directly nested attributes have translations, and others not. --- .wde_nextRelease.json | 27 +- CHANGELOG.md | 15 + dist/trunk/README.md | 15 + dist/trunk/classes/Admin_Message_Log.php | 66 +++ dist/trunk/classes/Import_Base.php | 209 ++++++-- dist/trunk/classes/Import_Posts.php | 367 +++++++++---- dist/trunk/classes/Import_Terms.php | 211 +++++--- dist/trunk/classes/Settings_Page.php | 120 +++-- dist/trunk/classes/Yaim.php | 4 +- dist/trunk/readme.txt | 15 + .../wp-background-processing/README.md | 158 ++++++ .../classes/wp-async-request.php | 163 ++++++ .../classes/wp-background-process.php | 506 ++++++++++++++++++ .../wp-background-processing/composer.json | 18 + .../wp-background-processing/license.txt | 280 ++++++++++ .../wp-background-processing.php | 20 + .../vendor/composer/autoload_classmap.php | 3 + .../trunk/vendor/composer/autoload_static.php | 3 + dist/trunk/vendor/composer/installed.json | 38 ++ dist/trunk/yaml-importer.php | 4 +- package.json | 2 +- src/languages/yaim-LOCALE.pot | 26 +- yaml-importer.php | 2 +- 23 files changed, 1918 insertions(+), 354 deletions(-) create mode 100644 dist/trunk/classes/Admin_Message_Log.php create mode 100644 dist/trunk/vendor/a5hleyrich/wp-background-processing/README.md create mode 100644 dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-async-request.php create mode 100644 dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-background-process.php create mode 100644 dist/trunk/vendor/a5hleyrich/wp-background-processing/composer.json create mode 100644 dist/trunk/vendor/a5hleyrich/wp-background-processing/license.txt create mode 100644 dist/trunk/vendor/a5hleyrich/wp-background-processing/wp-background-processing.php diff --git a/.wde_nextRelease.json b/.wde_nextRelease.json index 3201217..8d6505b 100644 --- a/.wde_nextRelease.json +++ b/.wde_nextRelease.json @@ -1,28 +1,3 @@ { - "changes": [ - { - "type": "fixed", - "message": "Multilang values for directly nested attributes. eg `meta_input`. Also works if some directly nested attributes have translations, and others not." - }, - { - "type": "added", - "message": "Handle `meta_input` field for Term imports" - }, - { - "type": "added", - "message": "Use `wpautop`for certain fields. Fields are filtered by \"yaim_{$type}_autop_keys\" filter" - }, - { - "type": "added", - "message": "Handle p2p connections for post import" - }, - { - "type": "changed", - "message": "More stable log" - }, - { - "type": "added", - "message": "Use a5hleyrich/wp-background-processing and process all imports in background" - } - ] + "changes": [] } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 811738c..f9cd25f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 0.1.0 - 2019-09-25 +Background Processing + +### Added +- Handle `meta_input` field for Term imports +- Use `wpautop`for certain fields. Fields are filtered by "yaim_{$type}_autop_keys" filter +- Handle p2p connections for post import +- Use a5hleyrich/wp-background-processing and process all imports in background + +### Changed +- More stable log + +### Fixed +- Multilang values for directly nested attributes. eg `meta_input`. Also works if some directly nested attributes have translations, and others not. + ## 0.0.2 - 2019-09-23 Taxonomy Term Import diff --git a/dist/trunk/README.md b/dist/trunk/README.md index 7d87649..eea058c 100644 --- a/dist/trunk/README.md +++ b/dist/trunk/README.md @@ -30,6 +30,21 @@ Upload and install this Theme the same way you'd install any other Theme. ## Changelog ## +## 0.1.0 - 2019-09-25 +Background Processing + +### Added +- Handle `meta_input` field for Term imports +- Use `wpautop`for certain fields. Fields are filtered by "yaim_{$type}_autop_keys" filter +- Handle p2p connections for post import +- Use a5hleyrich/wp-background-processing and process all imports in background + +### Changed +- More stable log + +### Fixed +- Multilang values for directly nested attributes. eg `meta_input`. Also works if some directly nested attributes have translations, and others not. + ## 0.0.2 - 2019-09-23 Taxonomy Term Import diff --git a/dist/trunk/classes/Admin_Message_Log.php b/dist/trunk/classes/Admin_Message_Log.php new file mode 100644 index 0000000..07a7045 --- /dev/null +++ b/dist/trunk/classes/Admin_Message_Log.php @@ -0,0 +1,66 @@ +start = date('d-M-Y H:i:s T'); + update_option( self::$option_key . '_start', $this->start ); + + $this->add_entries( array( $this->start, '' ) ); + + } + + public function add_entries( $msgs = array(), $key = null ){ + foreach( $msgs as $msg ) { + $this->add_entry( $msg ); + } + } + + public function add_entry( $msg = '', $key = null ){ + if ( null === $key ) { + $this->msgs[] = $msg; + } else { + if ( array_key_exists( $key, $this->msgs ) ) { + $this->msgs[$key] .= $msg; + + } else { + $this->msgs[$key] = $msg; + } + } + } + + public function get(){ + return $this->msgs; + } + + public static function get_from_db(){ + $msgs = get_option( self::$option_key ); + $last = get_option( self::$option_key . '_start' ); + return array( + 'msgs' => $msgs, + 'last_start' => get_option( self::$option_key . '_start' ), + 'last_log_saved' => count( $msgs ) > 0 && $msgs[0] === $last, + ); + } + + public function save(){ + update_option( self::$option_key, $this->msgs ); + } + +} + diff --git a/dist/trunk/classes/Import_Base.php b/dist/trunk/classes/Import_Base.php index 9b1e00e..159be7c 100644 --- a/dist/trunk/classes/Import_Base.php +++ b/dist/trunk/classes/Import_Base.php @@ -9,66 +9,124 @@ use croox\wde\utils; -abstract class Import_Base { +abstract class Import_Base extends \WP_Background_Process { - protected $objects = array(); + protected $items = array(); - protected $type = ''; // eg. post, term + protected static $type = ''; // eg. post, term - protected $active_langs = array(); + protected static $autop_keys = array(); - protected $log = array(); + protected $uploads_dir_path; - public function __construct( $objects ){ + // public function __construct(){ + // parent::__construct(); + // } - $this->active_langs = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ); - $this->active_langs = null === $this->active_langs ? $this->active_langs : array_map( function( $lang ) { - return $lang['language_code']; - }, $this->active_langs ); - - $this->setup_import_data( $objects ); + protected static function log( $msg = '' ) { + $path = WP_CONTENT_DIR . '/yaml-importer/import.log'; + error_log( + sprintf( '[%s] %s %s', date('d-M-Y H:i:s T'), $msg, PHP_EOL ), + 3, + $path + ); + } - $this->insert_objects(); + protected function task( $item_raw_data ) { - return $this; - } + static::log( '' ); + static::log( implode( ' ', array( + 'Start' . "\t", + static::$type, + 'import', + ) ) ); - abstract protected function setup_import_data( $file_data ); + $item = static::setup_import_data( $item_raw_data ); - abstract protected function classify_atts_by_validy( $object_atts, $use_deferred = false ); + $item = static::prepare_import_data( $item ); - protected function fix_insert_args( $atts ) { - return $atts; - } + static::log( implode( ' ', array( + 'Prepared' . "\t", + static::$type, + 'data', + ) ) ); - protected function insert_objects() { if ( class_exists( 'SitePress' ) ) { $current_lang = apply_filters( 'wpml_current_language', null ); do_action( 'wpml_switch_language', apply_filters( 'wpml_default_language', null ) ); } - foreach( $this->objects as $i => $object ) { - if ( is_wp_error( $object['atts'] ) ) - continue; - if ( $object['is_wpml_import'] ) { - $this->insert_object_wmpl( $i, $object ); + if ( ! is_wp_error( $item['atts'] ) ) { + + if ( $item['is_wpml_import'] ) { + $item = $this->insert_item_wmpl( $item ); } else { - $this->insert_object( $i, $object ); + $item = $this->insert_item( $item ); } + + } else { + static::log( implode( ' ', array( + 'ERROR', + $item['atts']->get_error_message() + ) ) ); } if ( class_exists( 'SitePress' ) ) { do_action( 'wpml_switch_language', $current_lang, null ); } + + static::log( 'Done' ); + static::log( '' ); + + return false; } - abstract protected function insert_object( $i, $object ); + abstract protected static function setup_import_data( $item ); + + // classify_atts_by_validy for wp_insert_post wp_insert_term function + // and fix_insert_args + protected static function prepare_import_data( $item ) { + if ( $item['is_wpml_import'] ) { + foreach( $item['atts'] as $lang => $atts ) { + if ( 'all' === $lang ) + continue; + + // merge atts recursive for all into current atts_by_lang + $atts = array_merge_recursive( + $atts, + utils\Arr::get( $item, 'atts.all', array() ) + ); + + $atts = static::classify_atts_by_validy( $atts, $item['is_wpml_import'] ); + + $atts = static::fix_insert_args( $atts ); + + $item['atts'][$lang] = apply_filters( "yaim_" . static::$type . "_atts", $atts, $lang, $item ); + } + } else { + $atts = static::classify_atts_by_validy( utils\Arr::get( $item, 'atts.all', array() ), $item['is_wpml_import'] ); + + $atts = static::fix_insert_args( $atts ); + + $item['atts']['all'] = apply_filters( "yaim_" . static::$type . "_atts", $atts, 'all', $item ); + } + + return $item; + } - abstract protected function insert_object_wmpl( $i, $object ); + abstract protected static function classify_atts_by_validy( $item_atts, $use_deferred = false ); - protected function is_wpml_import( $object_raw_data ) { + protected static function fix_insert_args( $atts ) { + return $atts; + } + + abstract protected function insert_item( $item ); + + abstract protected function insert_item_wmpl( $item ); + + protected static function is_wpml_import( $item_raw_data ) { $is_wpml_import = false; - foreach( $object_raw_data as $key => $attr ) { + foreach( $item_raw_data as $key => $attr ) { if ( $is_wpml_import ) break; $is_wpml_import = is_array( $attr ) @@ -77,7 +135,7 @@ protected function is_wpml_import( $object_raw_data ) { return $is_wpml_import; } - protected function classify_atts_by_lang( $object_raw_data, $not_translatable = array() ) { + protected static function classify_atts_by_lang( $item_raw_data, $not_translatable = array() ) { $atts = array( 'all' => array() ); @@ -87,53 +145,88 @@ protected function classify_atts_by_lang( $object_raw_data, $not_translatable = if ( null !== $default_lang ) $atts[$default_lang] = array(); - foreach( $object_raw_data as $key => $attr ) { + foreach( $item_raw_data as $key => $attr ) { if ( is_array( $attr ) ) { - $keys_starts_wpml_ = array_unique( array_map( function( $k ) { - return substr( $k, 0, strlen( 'wpml_' ) ) === 'wpml_'; - }, array_keys( $attr ) ) ); - if ( count( $keys_starts_wpml_ ) > 1 ) // something wrong, mixed language fields + $is_wpml_attr = static::is_wpml_attr( $key, array_keys( $attr ) ); + if ( is_wp_error( $is_wpml_attr ) ) { + static::log( 'ERROR: ' . $is_wpml_attr->get_error_message() ); continue; + } - if ( $keys_starts_wpml_[0] ) { // all keys start with wpml_ + if ( $is_wpml_attr ) { foreach( $attr as $lang => $val ) { $lang = str_replace( 'wpml_', '', $lang ); - if ( in_array( $lang, $this->active_langs ) ) { + if ( in_array( $lang, static::get_active_langs() ) ) { if ( in_array( $key, $not_translatable ) ) { return new \WP_Error( 'attr_not_translatable', sprintf( __( '"%s" is not translatable.', 'yaim' ), $key ) ); } else { - $atts[$lang][$key] = $val; + $atts[$lang][$key] = static::maybe_autop( $key, $val ); } } else { - // something wrong, lang not active + static::log( "ERROR: \$lang={$lang} not acive" ); + } + } + } else { + foreach( $attr as $n_key => $n_attr ) { + + if ( is_array( $n_attr ) ) { + + $is_wpml_attr = static::is_wpml_attr( $n_key, array_keys( $n_attr ) ); + if ( is_wp_error( $is_wpml_attr ) ) { + static::log( 'ERROR: ' . $is_wpml_attr->get_error_message() ); + continue; + } + + if ( $is_wpml_attr ) { + foreach( $n_attr as $lang => $val ) { + $lang = str_replace( 'wpml_', '', $lang ); + if ( in_array( $lang, static::get_active_langs() ) ) { + $atts[$lang][$key][$n_key] = static::maybe_autop( $n_key, $val ); + } else { + static::log( "ERROR: \$lang={$lang} not acive" ); + } + } + } else { // is normal field + $atts['all'][$key][$n_key] = static::maybe_autop( $n_key, $n_attr ); + } + + } else { // is normal field + $atts['all'][$key][$n_key] = static::maybe_autop( $n_key, $n_attr ); } } - } else { // is normal field - $atts['all'][$key] = $attr; } } else { // is normal field - $atts['all'][$key] = $attr; + $atts['all'][$key] = static::maybe_autop( $key, $attr ); } } return $atts; } - /** - * Public getter method for retrieving protected/private variables - * @param string $field Field to retrieve - * @return mixed Field value or exception is thrown - */ - public function __get( $field ) { - if ( in_array( $field, array( - 'objects', - 'type', - 'log', - ), true ) ) { - return $this->{$field}; - } - throw new Exception( 'Invalid property: ' . $field ); + protected static function maybe_autop( $key, $val ) { + $autop_keys = apply_filters( "yaim_" . static::$type . "_autop_keys", static::$autop_keys ); + return in_array( $key, $autop_keys ) + ? wpautop( $val ) + : $val; + } + + protected static function is_wpml_attr( $key, $keys ) { + $keys_starts_wpml_ = array_unique( array_map( function( $k ) { + return substr( $k, 0, strlen( 'wpml_' ) ) === 'wpml_'; + }, $keys ) ); + + if ( count( $keys_starts_wpml_ ) > 1 ) + return new \WP_Error( 'attr_mixed_language', sprintf( __( '"%s" has translated and non-translated input.', 'yaim' ), $key ) ); + + return $keys_starts_wpml_[0]; + } + + protected static function get_active_langs() { + $active_langs = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ); + return is_array( $active_langs ) + ? array_map( function( $lang ) { return $lang['language_code']; }, $active_langs ) + : array(); } } \ No newline at end of file diff --git a/dist/trunk/classes/Import_Posts.php b/dist/trunk/classes/Import_Posts.php index 353d19f..d371539 100644 --- a/dist/trunk/classes/Import_Posts.php +++ b/dist/trunk/classes/Import_Posts.php @@ -11,69 +11,37 @@ class Import_Posts extends Import_Base { - protected $type = 'post'; + protected $action = 'import_post'; - protected function setup_import_data( $objects ) { + protected static $type = 'post'; - // classify_atts_by_lang and check is_wpml_import - foreach( $objects as $object_i => $object_raw_data ) { + protected static $autop_keys = array( + 'post_content', + 'post_excerpt', + ); - // clean object_raw_data from not allowed fields - foreach( array( - 'ID', - 'guid', - ) as $not_allowed ) { - if ( array_key_exists( $not_allowed, $object_raw_data ) ) { - unset( $object_raw_data[$not_allowed] ); - } + protected static function setup_import_data( $item_raw_data ) { + // clean item_raw_data from not allowed fields + foreach( array( + 'ID', + 'guid', + ) as $not_allowed ) { + if ( array_key_exists( $not_allowed, $item_raw_data ) ) { + unset( $item_raw_data[$not_allowed] ); } - - // check if is_wpml_import for this object - $is_wpml_import = - $is_wpml_import = $this->is_wpml_import( $object_raw_data ); - - $this->objects[$object_i] = array( - 'is_wpml_import' => $is_wpml_import, - 'inserted' => array(), - 'atts' => $this->classify_atts_by_lang( $object_raw_data, array( - 'post_type', - // ??? may be more - ) ), - ); - } - // classify_atts_by_validy for wp_insert_post function - // and fix_insert_args - foreach( $this->objects as $object_i => $object ) { - if ( $object['is_wpml_import'] ) { - foreach( $object['atts'] as $lang => $atts ) { - if ( 'all' === $lang ) - continue; - - // merge atts for all into current atts_by_lang - $atts = array_merge( - $atts, - utils\Arr::get( $this->objects, $object_i . '.atts.all', array() ) - ); - - $atts = $this->classify_atts_by_validy( $atts, $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts'][$lang] = apply_filters( "yaim_{$this->type}_atts", $atts, $lang, $object ); // ??? should add type as param - } - } else { - $atts = $this->classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts']['all'] = apply_filters( "yaim_{$this->type}_atts", $atts, 'all', $object ); - } - } + return array( + 'is_wpml_import' => static::is_wpml_import( $item_raw_data ), + 'inserted' => array(), + 'atts' => static::classify_atts_by_lang( $item_raw_data, array( + 'post_type', + // ??? may be more + ) ), + ); } - protected function classify_atts_by_validy( $object_atts, $use_deferred = false ) { + protected static function classify_atts_by_validy( $item_atts, $use_deferred = false ) { $valid_insert_args = array( 'post_author', @@ -117,7 +85,7 @@ protected function classify_atts_by_validy( $object_atts, $use_deferred = false 'custom_args' => array(), ); - foreach( $object_atts as $key => $val ) { + foreach( $item_atts as $key => $val ) { if ( $use_deferred && in_array( $key, $deferred_insert_args ) ) { $classified_atts['deferred_insert_args'][$key] = $val; } elseif ( in_array( $key, $valid_insert_args ) ) { @@ -130,7 +98,7 @@ protected function classify_atts_by_validy( $object_atts, $use_deferred = false return $classified_atts; } - protected function fix_insert_args( $atts ) { + protected static function fix_insert_args( $atts ) { foreach( array( 'insert_args', @@ -171,67 +139,122 @@ protected function fix_insert_args( $atts ) { return $atts; } - protected function insert_object_wmpl( $i, $object ) { + protected function insert_item_wmpl( $item ) { if ( class_exists( 'SitePress' ) ) { global $sitepress; } - $object_trid = null; + $item_trid = null; $original_id = null; - foreach( $object['atts'] as $lang => $atts_by_lang ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; - $object_id = wp_insert_post( $atts_by_lang['insert_args'] ); + $item_id = wp_insert_post( $atts_by_lang['insert_args'] ); - if ( is_wp_error( $object_id ) ) { - $this->log[] = 'ERROR: ' . $object_id->get_error_message(); + if ( is_wp_error( $item_id ) ) { + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + 'Message: ' . $item_id->get_error_message() + ) ) ); continue; } - // original_id of first inserted object - $original_id = null === $original_id ? $object_id : $original_id; + // original_id of first inserted item + $original_id = null === $original_id ? $item_id : $original_id; - $object['inserted'][$lang] = $object_id; - $this->objects[$i]['inserted'][$lang] = $object_id; + $item['inserted'][$lang] = $item_id; - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $item_id, + ) ) ); - // object_trid of first inserted object - $object_trid = null === $object_trid - ? $sitepress->get_element_trid( $object_id ) - : $object_trid; + // item_trid of first inserted item + $item_trid = null === $item_trid + ? $sitepress->get_element_trid( $item_id ) + : $item_trid; - do_action( "yaim_{$this->type}_inserted_for_wpml_translation", - $object_id, - $this->objects[$i], - $lang, - $object_trid - ); + // do_action( "yaim_" . static::$type . "_inserted_for_wpml_translation", + // $item_id, + // $item, + // $lang, + // $item_trid + // ); $translation_id = $sitepress->set_element_language_details( - $object_id, - 'post_' . get_post_type( $object_id ), - $object_trid, + $item_id, + 'post_' . get_post_type( $item_id ), + $item_trid, $lang ); - $this->log[$object_id . '_in'] .= " \$lang={$lang} \$translation_id={$translation_id}"; - $this->log[$object_id . '_in'] .= $original_id === $object_id ? '' : " as translation for \$id={$original_id}"; + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $item_id, + '$lang=' . $lang, + '$translation_id=' . $translation_id, + $original_id === $item_id ? '' : 'as translation for $id=' . $original_id, + ) ) ); + + // do_action( "yaim_" . static::$type . "_wpml_language_set", + // $item_id, + // $item, + // $lang, + // $item_trid, + // $translation_id + // ); - do_action( "yaim_{$this->type}_wpml_language_set", - $object_id, - $this->objects[$i], - $lang, - $object_trid, - $translation_id - ); + } + + + $item = $this->update_item_deferred( $item ); + + $item = $this->update_item_p2p( $item ); + + // if ( isset( $item_id ) ) + // do_action( "yaim_" . static::$type . "_inserted", $item, $item_id ); // ??? needs $i + return $item; + } + + protected function insert_item( $item ) { + $item_id = wp_insert_post( array_merge( + utils\Arr::get( $item, 'atts.all.insert_args', array() ), + utils\Arr::get( $item, 'atts.all.deferred_insert_args', array() ) + ) ); + + if ( is_wp_error( $item_id ) ) { + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + 'Message: ' . $item_id->get_error_message() + ) ) ); + return; } - // do the deferred stuff - foreach( $object['atts'] as $lang => $atts_by_lang ) { + $item['inserted']['all'] = $item_id; + + $this->update_item_p2p( $item ); + + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $item_id, + ) ) ); + + // do_action( "yaim_" . static::$type . "_inserted", $item, $item_id ); + } + + protected function update_item_deferred( $item ) { + + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; @@ -239,15 +262,15 @@ protected function insert_object_wmpl( $i, $object ) { || empty( $atts_by_lang['deferred_insert_args'] ) ) continue; - $object_id = utils\Arr::get( $object, 'inserted.' . $lang, false ); + $item_id = utils\Arr::get( $item, 'inserted.' . $lang, false ); - if ( ! $object_id ) + if ( ! $item_id ) continue; $args = array_merge( array( - 'ID' => $object_id, - 'post_type' => get_post_type( $object_id ), + 'ID' => $item_id, + 'post_type' => get_post_type( $item_id ), ), $atts_by_lang['deferred_insert_args'] ); @@ -258,36 +281,152 @@ protected function insert_object_wmpl( $i, $object ) { $updated = wp_update_post( $args, true ); if ( is_wp_error( $updated ) ) { - $this->log[] = "ERROR updating {$object_id}: " . $updated->get_error_message(); + static::log( implode( ' ', array( + 'WARNING' . "\t", + 'updating', + '$id=' . $item_id, + static::$type, + $updated->get_error_message(), + ) ) ); continue; } - $this->log[$object_id . '_up'] = "Updated {$this->type} \$id={$object_id} \$args=[" . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . "] and wpml should have done the sync"; - } + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $item_id, + '$args=[' . implode( ', ', array_keys( $atts_by_lang['deferred_insert_args'] ) ) . ']', + 'and wpml should have done the sync', + ) ) ); - if ( isset( $object_id ) ) - $this->log[$object_id . '_after'] = ''; - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + } + return $item; } - protected function insert_object( $i, $object ) { - $object_id = wp_insert_post( array_merge( - utils\Arr::get( $object, 'atts.all.insert_args', array() ), - utils\Arr::get( $object, 'atts.all.deferred_insert_args', array() ) - ) ); + protected function update_item_p2p( $item ) { + if ( class_exists( 'P2P_Connection_Type_Factory' ) ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { + if ( 'all' === $lang ) + continue; - if ( is_wp_error( $object_id ) ) { - $this->log[] = 'ERROR: ' . $object_id->get_error_message(); - return; - } + $p2p = utils\Arr::get( $atts_by_lang,'custom_args.p2p' ); + + if ( ! $p2p ) + continue; + + $item_id = utils\Arr::get( $item, 'inserted.' . $lang, false ); + + if ( ! $item_id ) + continue; + + foreach( $p2p as $ctype_name => $conns ) { + + $ctype = \P2P_Connection_Type_Factory::get_instance( $ctype_name ); + + $item_side = false; + $item_post_type = get_post_type( $item_id ); + $both_sides_are_posttype = true; + $conn_post_type = false; + foreach( $ctype->side as $direction => $side ) { + if ( ! $side instanceof \P2P_Side_Post ) { + $both_sides_are_posttype = false; + continue; + } + if ( in_array( $item_post_type, utils\Arr::get( $side->query_vars, 'post_type' ) ) ) { + $item_side = ! $item_side + ? $direction + : $item_side; + } else { + $conn_post_type = ! $conn_post_type + ? utils\Arr::get( $side->query_vars, 'post_type.0' ) + : $conn_post_type; + } + } - $this->objects[$i]['inserted']['all'] = $object_id; + if ( ! $both_sides_are_posttype ) { + static::log( implode( ' ', array( + 'WARNING' . "\t", + 'updating', + static::$type, + 'Currently p2p connections are only supported, if both connection sides represent a posttype', + ) ) ); + continue; + } - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + if ( ! $item_side ) { + static::log( implode( ' ', array( + 'WARNING' . "\t", + 'updating', + static::$type, + 'No side of $connection_type=' . $ctype_name . ' represents $posttype=' . $item_post_type, + ) ) ); + continue; + } - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], null ); + $connected_post_ids = array(); + foreach( $conns as $conn_k => $conn_v ) { + + $conn_post_slug = is_array( $conn_v ) || ( is_string( $conn_v ) && empty( $conn_v ) ) + ? $conn_k + : $conn_v; + + $get_post_args = array( + 'name' => $conn_post_slug, + 'posts_per_page' => 1, + 'post_type' => $conn_post_type, + 'fields' => 'ids', + ); + $get_post_args = class_exists( 'SitePress' ) ? array_merge( $get_post_args, array( + 'lang' => apply_filters( 'wpml_current_language', null ), + 'suppress_filters' => false, + ) ) : $get_post_args; + + $conn_post_id = utils\Arr::get( get_posts( $get_post_args ), '0', false ); + + if ( ! $conn_post_id ) { + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'updating', + static::$type, + '$id=' . $item_id, + 'can not create p2p connection, can not find post with $slug=' . $conn_post_slug, + ) ) ); + continue; + } + + // create connection + $p2p_id = $ctype->connect( + 'from' === $item_side ? $item_id : $conn_post_id, + 'to' === $item_side ? $item_id : $conn_post_id, + is_array( $conn_v ) ? $conn_v : array() + ); + + if ( is_wp_error( $p2p_id ) ) { + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'creating p2p connection', + static::$type, + '$id=' . $item_id, + 'Message: ' . $item_id->get_error_message() + ) ) ); + continue; + } + $connected_post_ids[] = $conn_post_id; + } + + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $item_id, + 'p2p connected with post $ids=[' . implode( ', ', $connected_post_ids ) . ']', + '$ctype_name=' . $ctype_name, + ) ) ); + } + } + } + return $item; } } \ No newline at end of file diff --git a/dist/trunk/classes/Import_Terms.php b/dist/trunk/classes/Import_Terms.php index 5064bc7..8743596 100644 --- a/dist/trunk/classes/Import_Terms.php +++ b/dist/trunk/classes/Import_Terms.php @@ -11,55 +11,25 @@ class Import_Terms extends Import_Base { - protected $type = 'term'; + protected $action = 'import_term'; - protected function setup_import_data( $objects ) { + protected static $type = 'term'; - // classify_atts_by_lang and check is_wpml_import - foreach( $objects as $object_i => $object_raw_data ) { + protected static $autop_keys = array( + 'description', + ); - // check if is_wpml_import for this object - $is_wpml_import = $this->is_wpml_import( $object_raw_data ); - - $this->objects[$object_i] = array( - 'is_wpml_import' => $is_wpml_import, - 'inserted' => array(), - 'atts' => $this->classify_atts_by_lang( $object_raw_data, array() ), - ); - - } - - // classify_atts_by_validy for wp_insert_term function - // and fix_insert_args - foreach( $this->objects as $object_i => $object ) { - if ( $object['is_wpml_import'] ) { - foreach( $object['atts'] as $lang => $atts ) { - if ( 'all' === $lang ) - continue; - - // merge atts for all into current atts_by_lang - $atts = array_merge( - $atts, - utils\Arr::get( $this->objects, $object_i . '.atts.all', array() ) - ); - - $atts = $this->classify_atts_by_validy( $atts, $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts'][$lang] = apply_filters( "yaim_{$this->type}_atts", $atts, $lang, $object ); - } - } else { - $atts = $this->classify_atts_by_validy( utils\Arr::get( $object, 'atts.all', array() ), $object['is_wpml_import'] ); - - $atts = $this->fix_insert_args( $atts ); - - $this->objects[$object_i]['atts']['all'] = apply_filters( "yaim_{$this->type}_atts", $atts, 'all', $object ); - } - } + protected static function setup_import_data( $item_raw_data ) { + return array( + 'is_wpml_import' => static::is_wpml_import( $item_raw_data ), + 'inserted' => array(), + 'atts' => static::classify_atts_by_lang( $item_raw_data, array( + 'taxonomy', + ) ), + ); } - protected function classify_atts_by_validy( $object_atts, $use_deferred = false ) { + protected static function classify_atts_by_validy( $item_atts, $use_deferred = false ) { $valid_insert_args = array( 'name', @@ -74,7 +44,7 @@ protected function classify_atts_by_validy( $object_atts, $use_deferred = false 'custom_args' => array(), ); - foreach( $object_atts as $key => $val ) { + foreach( $item_atts as $key => $val ) { if ( in_array( $key, $valid_insert_args ) ) { $classified_atts['insert_args'][$key] = $val; } else { @@ -85,7 +55,7 @@ protected function classify_atts_by_validy( $object_atts, $use_deferred = false return $classified_atts; } - protected function fix_insert_args( $atts ) { + protected static function fix_insert_args( $atts ) { foreach( array( 'parent', @@ -106,15 +76,15 @@ protected function fix_insert_args( $atts ) { return $atts; } - protected function insert_object_wmpl( $i, $object ) { + protected function insert_item_wmpl( $item ) { if ( class_exists( 'SitePress' ) ) { global $sitepress; } - $object_trid = null; + $item_trid = null; $original_id = null; - foreach( $object['atts'] as $lang => $atts_by_lang ) { + foreach( $item['atts'] as $lang => $atts_by_lang ) { if ( 'all' === $lang ) continue; @@ -125,76 +95,141 @@ protected function insert_object_wmpl( $i, $object ) { $term_tax_ids = wp_insert_term( $name, $taxonomy, $args ); if ( is_wp_error( $term_tax_ids ) ) { - $this->log[] = 'ERROR: ' . $term_tax_ids->get_error_message(); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + 'Message: ' . $term_tax_ids->get_error_message() + ) ) ); continue; } - $object_id = $term_tax_ids['term_id']; + $item_id = $term_tax_ids['term_id']; - // original_id of first inserted object - $original_id = null === $original_id ? $object_id : $original_id; + // original_id of first inserted item + $original_id = null === $original_id ? $item_id : $original_id; - $object['inserted'][$lang] = $object_id; - $this->objects[$i]['inserted'][$lang] = $object_id; + $item['inserted'][$lang] = $item_id; - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $item_id, + ) ) ); - // object_trid of first inserted object - $object_trid = null === $object_trid - ? apply_filters( 'wpml_element_trid', NULL, $object_id, 'tax_' . $taxonomy ) - : $object_trid; + // item_trid of first inserted item + $item_trid = null === $item_trid + ? apply_filters( 'wpml_element_trid', NULL, $item_id, 'tax_' . $taxonomy ) + : $item_trid; - do_action( "yaim_{$this->type}_inserted_for_wpml_translation", - $object_id, - $this->objects[$i], - $lang, - $object_trid - ); + // do_action( "yaim_{$this->type}_inserted_for_wpml_translation", + // $item_id, + // $this->items[$i], + // $lang, + // $item_trid + // ); $translation_id = $sitepress->set_element_language_details( - $object_id, + $item_id, 'tax_' . $taxonomy, - $object_trid, + $item_trid, $lang ); - $this->log[$object_id . '_in'] .= " \$lang={$lang} \$translation_id={$translation_id}"; - $this->log[$object_id . '_in'] .= $original_id === $object_id ? '' : " as translation for \$id={$original_id}"; - - do_action( "yaim_{$this->type}_wpml_language_set", - $object_id, - $this->objects[$i], - $lang, - $object_trid, - $translation_id - ); + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $item_id, + '$lang=' . $lang, + '$translation_id=' . $translation_id, + $original_id === $item_id ? '' : 'as translation for $id=' . $original_id, + ) ) ); + + // do_action( "yaim_{$this->type}_wpml_language_set", + // $item_id, + // $this->items[$i], + // $lang, + // $item_trid, + // $translation_id + // ); } - if ( isset( $object_id ) && is_numeric( $object_id ) ) - $this->log[$object_id . '_after'] = ''; + // handle meta_input + foreach( $item['atts'] as $lang => $atts_by_lang ) { + if ( 'all' === $lang ) + continue; + + $meta_input = utils\Arr::get( $atts_by_lang,'custom_args.meta_input' ); + + if ( ! $meta_input ) + continue; + + $item_id = utils\Arr::get( $item, 'inserted.' . $lang, false ); + + if ( ! $item_id ) + continue; + + foreach( $meta_input as $meta_key => $meta_val ) { + $updated = update_term_meta( $item_id, $meta_key, $meta_val ); + if ( is_numeric( $updated ) && $updated == ( int ) $updated ) { + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $item_id, + 'added $meta_key=' . $meta_key . ' $meta_id ' . $updated, + ) ) ); + } elseif ( $updated ) { + static::log( implode( ' ', array( + 'Updated' . "\t", + static::$type, + '$id=' . $item_id, + 'updated $meta_key=' . $meta_key, + ) ) ); + } else { + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'updating', + static::$type, + '$id=' . $item_id, + 'could not add $meta_key=' . $meta_key, + ) ) ); + continue; + } + } + } - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], $object_id ); + // if ( isset( $item_id ) ) + // do_action( "yaim_{$this->type}_inserted", $this->items[$i], $item_id ); } - protected function insert_object( $i, $object ) { - $args = utils\Arr::get( $object, 'atts.all.insert_args', array() ); + protected function insert_item( $item ) { + $args = utils\Arr::get( $item, 'atts.all.insert_args', array() ); $name = array_splice( $args, array_search( 'name', array_keys( $args ) ), 1 )['name']; $taxonomy = array_splice( $args, array_search( 'taxonomy', array_keys( $args ) ), 1 )['taxonomy']; $term_tax_ids = wp_insert_term( $name, $taxonomy, $args ); if ( is_wp_error( $term_tax_ids ) ) { - $this->log[] = 'ERROR: ' . $term_tax_ids->get_error_message(); + static::log( implode( ' ', array( + 'ERROR' . "\t", + 'inserting', + static::$type, + 'Message: ' . $term_tax_ids->get_error_message() + ) ) ); return; } - $object_id = $term_tax_ids['term_id']; - $this->objects[$i]['inserted']['all'] = $object_id; + $item_id = $term_tax_ids['term_id']; + $item['inserted']['all'] = $item_id; - $this->log[$object_id . '_in'] = "Inserted {$this->type} \$id={$object_id}"; + static::log( implode( ' ', array( + 'Inserted' . "\t", + static::$type, + '$id=' . $item_id, + ) ) ); - do_action( "yaim_{$this->type}_inserted", $this->objects[$i], null ); + // do_action( "yaim_{$this->type}_inserted", $this->items[$i], null ); } } \ No newline at end of file diff --git a/dist/trunk/classes/Settings_Page.php b/dist/trunk/classes/Settings_Page.php index 49be126..d5c8858 100644 --- a/dist/trunk/classes/Settings_Page.php +++ b/dist/trunk/classes/Settings_Page.php @@ -13,32 +13,39 @@ class Settings_Page { protected $main_class; + protected $uploads_dir_path; + protected $cmb_id = 'yaim_options'; + protected $import_posts; + protected $import_terms; + + protected $suported_types = array( + 'posts', + 'terms', + ); + protected static $instance = null; - public static function get_instance( $args = array() ) { + public static function get_instance() { if ( null === self::$instance ) { - $required_args = array( - 'main_class', - ); - foreach( $required_args as $required_arg ) { - if ( ! array_key_exists( $required_arg, $args ) || empty( $args[$required_arg] ) ) { - error_log( __FILE__ ); - return new \WP_Error( 'missing_arg', sprintf( __( 'Argument "%s" missing', 'yaim' ), $required_arg ) ); - } - } - self::$instance = new self( $args ); + self::$instance = new self(); } return self::$instance; } - - public function __construct( $args ){ - $this->main_class = $args['main_class']; + public function __construct(){ add_action( 'cmb2_admin_init', array( $this, 'options_page_metabox' ) ); add_action( 'cmb2_options-page_process_fields_' . $this->cmb_id, array( $this, 'process_fields' ), 10, 2 ); + add_action( 'init', array( $this, 'init_importers' ) ); + } + public function init_importers() { + foreach( $this->suported_types as $type ) { + $importer = 'import_' . $type; + $importer_class = __NAMESPACE__ . '\Import_' . ucfirst( $type ); + $this->$importer = new $importer_class(); + } } public function options_page_metabox() { @@ -64,7 +71,7 @@ public function options_page_metabox() { // 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty. // 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page. // 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()). - // 'save_button' => esc_html__( 'Save Theme Options', 'yaim' ), // The text for the options-page save button. Defaults to 'Save'. + 'save_button' => esc_html__( 'Start Import', 'yaim' ), // The text for the options-page save button. Defaults to 'Save'. // 'disable_settings_errors' => true, // On settings pages (not options-general.php sub-pages), allows disabling. 'message_cb' => array( $this, 'message_cb' ), // 'tab_group' => '', // Tab-group identifier, enables options page tab navigation. @@ -83,24 +90,16 @@ public function options_page_metabox() { 'options_cb' => array( $this, 'options_cb' ), ) ); - // empty field, useful to save the log later - $cmb->add_field( array( - 'id' => 'log', - 'type' => 'select', // select because it will sanitize and save array - 'render_row_cb' => array( $this, 'render_empty_field' ), - ) ); } function render_empty_field( $field_args, $field ) {} public function options_cb( $field ) { - $uploads_dir_path = WP_CONTENT_DIR . '/' . call_user_func( array( $this->main_class, 'get_instance' ) )->slug; - $files = glob( $uploads_dir_path . '/*'); - - $labels = array_map( function( $file ) use ( $uploads_dir_path ) { - return str_replace( $uploads_dir_path . '/', '', $file ); + $path = WP_CONTENT_DIR . '/yaml-importer/'; + $files = glob( $path .'*.yaml'); + $labels = array_map( function( $file ) use ( $path ) { + return str_replace( $path, '', $file ); }, $files ); - return array_combine( $files, $labels ); } @@ -108,6 +107,10 @@ public function options_cb( $field ) { // Do some processing just before the fileds are saved public function process_fields( $cmb, $cmb_id ) { + $admin_message_log = new Admin_Message_Log(); + + $admin_message_log->add_entry( 'Start Import' ); + $file = utils\Arr::get( $cmb->data_to_save, 'file' ); if ( empty( $file ) ) @@ -117,22 +120,33 @@ public function process_fields( $cmb, $cmb_id ) { // ??? validate $file_data - $cmb->data_to_save['log'] = array(); - foreach( $file_data as $type => $objects ) { - switch( $type ) { - case 'posts': - $importer = new Import_Posts( $objects ); - $cmb->data_to_save['log'] = array_merge( $cmb->data_to_save['log'], $importer->log ); - break; - case 'terms': - $importer = new Import_Terms( $objects ); - $cmb->data_to_save['log'] = array_merge( $cmb->data_to_save['log'], $importer->log ); - break; - default: - $cmb->data_to_save['log'][] = "ERROR: import for {$type} not supported"; + $types_queued = array(); + foreach( $file_data as $type => $items ) { + if( ! in_array( $type, $this->suported_types ) ) { + $admin_message_log->add_entry( 'ERROR: Import for ' . $type . ' not supported' ); + continue; + } + + $importer = 'import_' . $type; + + foreach( $items as $item_raw_data ) { + $this->$importer->push_to_queue( $item_raw_data ); } + $this->$importer->save()->dispatch(); + $types_queued[] = $type; + + $admin_message_log->add_entry( 'All ' . count( $items ) . ' ' . $type . ' queued for import' ); + } + + $log_path = WP_CONTENT_DIR . '/yaml-importer/import.log'; + + if ( count( $types_queued ) ) { + $admin_message_log->add_entry( 'Start import ' . implode( ', ', $types_queued ) . ' in background, check ' . $log_path ); + } else { + $admin_message_log->add_entry( 'Nothing imported' ); } + $admin_message_log->save(); } /** @@ -161,23 +175,19 @@ public function message_cb( $cmb, $args ) { if ( empty( $args['should_notify'] ) ) return; - $log = utils\Arr::get( get_option( $this->cmb_id ), 'log', array() ); - - if ( ! $args['is_updated'] ) - $log[] = '

??? maybe something went wrong

'; + $admin_message_log = Admin_Message_Log::get_from_db(); - $log[] = 'Log saved to db $option_name=' . $this->cmb_id; + $log_path = WP_CONTENT_DIR . '/yaml-importer/import.log'; - $args['message'] = implode( '
', array_map( function( $msg ) { - return is_string( $msg ) && strlen( $msg ) > 0 - ? $msg . ';' - : $msg; - }, $log ) ); - - - $args['type'] = strpos( $args['message'], 'ERROR' ) !== false - ? 'notice-warning' - : $args['type']; + if ( $admin_message_log['last_log_saved'] ) { + $args['message'] = implode( '
', $admin_message_log['msgs'] ); + $args['type'] = strpos( $args['message'], 'ERROR' ) !== false + ? 'notice-warning' + : 'updated'; + } else { + $args['message'] = 'Something went wrong'; + $args['type'] = 'error'; + } add_settings_error( $args['setting'], $args['code'], $args['message'], $args['type'] ); } diff --git a/dist/trunk/classes/Yaim.php b/dist/trunk/classes/Yaim.php index 6d5b118..add9af1 100644 --- a/dist/trunk/classes/Yaim.php +++ b/dist/trunk/classes/Yaim.php @@ -14,9 +14,7 @@ class Yaim extends wde\Plugin { public function hooks(){ parent::hooks(); - Settings_Page::get_instance( array( - 'main_class' => __CLASS__, - ) ); + Settings_Page::get_instance(); } } \ No newline at end of file diff --git a/dist/trunk/readme.txt b/dist/trunk/readme.txt index 5dd1514..a93de95 100644 --- a/dist/trunk/readme.txt +++ b/dist/trunk/readme.txt @@ -30,6 +30,21 @@ Upload and install this Theme the same way you'd install any other Theme. == Changelog == +## 0.1.0 - 2019-09-25 +Background Processing + +### Added +- Handle `meta_input` field for Term imports +- Use `wpautop`for certain fields. Fields are filtered by "yaim_{$type}_autop_keys" filter +- Handle p2p connections for post import +- Use a5hleyrich/wp-background-processing and process all imports in background + +### Changed +- More stable log + +### Fixed +- Multilang values for directly nested attributes. eg `meta_input`. Also works if some directly nested attributes have translations, and others not. + ## 0.0.2 - 2019-09-23 Taxonomy Term Import diff --git a/dist/trunk/vendor/a5hleyrich/wp-background-processing/README.md b/dist/trunk/vendor/a5hleyrich/wp-background-processing/README.md new file mode 100644 index 0000000..04d74cc --- /dev/null +++ b/dist/trunk/vendor/a5hleyrich/wp-background-processing/README.md @@ -0,0 +1,158 @@ +# WP Background Processing + +WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks. Check out the [example plugin](https://github.com/A5hleyRich/wp-background-processing-example) or read the [accompanying article](https://deliciousbrains.com/background-processing-wordpress/). + +Inspired by [TechCrunch WP Asynchronous Tasks](https://github.com/techcrunch/wp-async-task). + +__Requires PHP 5.2+__ + +### Async Request + +Async requests are useful for pushing slow one-off tasks such as sending emails to a background process. Once the request has been dispatched it will process in the background instantly. + +Extend the `WP_Async_Request` class: + +```php +class WP_Example_Request extends WP_Async_Request { + + /** + * @var string + */ + protected $action = 'example_request'; + + /** + * Handle + * + * Override this method to perform any actions required + * during the async request. + */ + protected function handle() { + // Actions to perform + } + +} +``` + +##### `protected $action` + +Should be set to a unique name. + +##### `protected function handle()` + +Should contain any logic to perform during the non-blocking request. The data passed to the request will be accessible via `$_POST`. + +##### Dispatching Requests + +Instantiate your request: + +`$this->example_request = new WP_Example_Request();` + +Add data to the request if required: + +`$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );` + +Fire off the request: + +`$this->example_request->dispatch();` + +Chaining is also supported: + +`$this->example_request->data( array( 'data' => $data ) )->dispatch();` + +### Background Process + +Background processes work in a similar fashion to async requests but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed the next batch will start instantly. + +Health checks run by default every 5 minutes to ensure the queue is running when queued items exist. If the queue has failed it will be restarted. + +Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing. + +Extend the `WP_Background_Process` class: + +```php +class WP_Example_Process extends WP_Background_Process { + + /** + * @var string + */ + protected $action = 'example_process'; + + /** + * Task + * + * Override this method to perform any actions required on each + * queue item. Return the modified item for further processing + * in the next pass through. Or, return false to remove the + * item from the queue. + * + * @param mixed $item Queue item to iterate over + * + * @return mixed + */ + protected function task( $item ) { + // Actions to perform + + return false; + } + + /** + * Complete + * + * Override if applicable, but ensure that the below actions are + * performed, or, call parent::complete(). + */ + protected function complete() { + parent::complete(); + + // Show notice to user or perform some other arbitrary task... + } + +} +``` + +##### `protected $action` + +Should be set to a unique name. + +##### `protected function task( $item )` + +Should contain any logic to perform on the queued item. Return `false` to remove the item from the queue or return `$item` to push it back onto the queue for further processing. If the item has been modified and is pushed back onto the queue the current state will be saved before the batch is exited. + +##### `protected function complete()` + +Optionally contain any logic to perform once the queue has completed. + +##### Dispatching Processes + +Instantiate your process: + +`$this->example_process = new WP_Example_Process();` + +Push items to the queue: + +```php +foreach ( $items as $item ) { + $this->example_process->push_to_queue( $item ); +} +``` + +Save and dispatch the queue: + +`$this->example_process->save()->dispatch();` + +### BasicAuth + +If your site is behind BasicAuth, both async requests and background processes will fail to complete. This is because WP Background Processing relies on the [WordPress HTTP API](http://codex.wordpress.org/HTTP_API), which requires you to attach your BasicAuth credentials to requests. The easiest way to do this is using the following filter: + +```php +function wpbp_http_request_args( $r, $url ) { + $r['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD ); + + return $r; +} +add_filter( 'http_request_args', 'wpbp_http_request_args', 10, 2); +``` + +## License + +[GPLv2+](http://www.gnu.org/licenses/gpl-2.0.html) diff --git a/dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-async-request.php b/dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-async-request.php new file mode 100644 index 0000000..f1bef83 --- /dev/null +++ b/dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-async-request.php @@ -0,0 +1,163 @@ +identifier = $this->prefix . '_' . $this->action; + + add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); + add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); + } + + /** + * Set data used during the request + * + * @param array $data Data. + * + * @return $this + */ + public function data( $data ) { + $this->data = $data; + + return $this; + } + + /** + * Dispatch the async request + * + * @return array|WP_Error + */ + public function dispatch() { + $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); + $args = $this->get_post_args(); + + return wp_remote_post( esc_url_raw( $url ), $args ); + } + + /** + * Get query args + * + * @return array + */ + protected function get_query_args() { + if ( property_exists( $this, 'query_args' ) ) { + return $this->query_args; + } + + return array( + 'action' => $this->identifier, + 'nonce' => wp_create_nonce( $this->identifier ), + ); + } + + /** + * Get query URL + * + * @return string + */ + protected function get_query_url() { + if ( property_exists( $this, 'query_url' ) ) { + return $this->query_url; + } + + return admin_url( 'admin-ajax.php' ); + } + + /** + * Get post args + * + * @return array + */ + protected function get_post_args() { + if ( property_exists( $this, 'post_args' ) ) { + return $this->post_args; + } + + return array( + 'timeout' => 0.01, + 'blocking' => false, + 'body' => $this->data, + 'cookies' => $_COOKIE, + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), + ); + } + + /** + * Maybe handle + * + * Check for correct nonce and pass to handler. + */ + public function maybe_handle() { + // Don't lock up other requests while processing + session_write_close(); + + check_ajax_referer( $this->identifier, 'nonce' ); + + $this->handle(); + + wp_die(); + } + + /** + * Handle + * + * Override this method to perform any actions required + * during the async request. + */ + abstract protected function handle(); + + } +} diff --git a/dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-background-process.php b/dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-background-process.php new file mode 100644 index 0000000..03e3a3e --- /dev/null +++ b/dist/trunk/vendor/a5hleyrich/wp-background-processing/classes/wp-background-process.php @@ -0,0 +1,506 @@ +cron_hook_identifier = $this->identifier . '_cron'; + $this->cron_interval_identifier = $this->identifier . '_cron_interval'; + + add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); + add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); + } + + /** + * Dispatch + * + * @access public + * @return void + */ + public function dispatch() { + // Schedule the cron healthcheck. + $this->schedule_event(); + + // Perform remote post. + return parent::dispatch(); + } + + /** + * Push to queue + * + * @param mixed $data Data. + * + * @return $this + */ + public function push_to_queue( $data ) { + $this->data[] = $data; + + return $this; + } + + /** + * Save queue + * + * @return $this + */ + public function save() { + $key = $this->generate_key(); + + if ( ! empty( $this->data ) ) { + update_site_option( $key, $this->data ); + } + + return $this; + } + + /** + * Update queue + * + * @param string $key Key. + * @param array $data Data. + * + * @return $this + */ + public function update( $key, $data ) { + if ( ! empty( $data ) ) { + update_site_option( $key, $data ); + } + + return $this; + } + + /** + * Delete queue + * + * @param string $key Key. + * + * @return $this + */ + public function delete( $key ) { + delete_site_option( $key ); + + return $this; + } + + /** + * Generate key + * + * Generates a unique key based on microtime. Queue items are + * given a unique key so that they can be merged upon save. + * + * @param int $length Length. + * + * @return string + */ + protected function generate_key( $length = 64 ) { + $unique = md5( microtime() . rand() ); + $prepend = $this->identifier . '_batch_'; + + return substr( $prepend . $unique, 0, $length ); + } + + /** + * Maybe process queue + * + * Checks whether data exists within the queue and that + * the process is not already running. + */ + public function maybe_handle() { + // Don't lock up other requests while processing + session_write_close(); + + if ( $this->is_process_running() ) { + // Background process already running. + wp_die(); + } + + if ( $this->is_queue_empty() ) { + // No data to process. + wp_die(); + } + + check_ajax_referer( $this->identifier, 'nonce' ); + + $this->handle(); + + wp_die(); + } + + /** + * Is queue empty + * + * @return bool + */ + protected function is_queue_empty() { + global $wpdb; + + $table = $wpdb->options; + $column = 'option_name'; + + if ( is_multisite() ) { + $table = $wpdb->sitemeta; + $column = 'meta_key'; + } + + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; + + $count = $wpdb->get_var( $wpdb->prepare( " + SELECT COUNT(*) + FROM {$table} + WHERE {$column} LIKE %s + ", $key ) ); + + return ( $count > 0 ) ? false : true; + } + + /** + * Is process running + * + * Check whether the current process is already running + * in a background process. + */ + protected function is_process_running() { + if ( get_site_transient( $this->identifier . '_process_lock' ) ) { + // Process already running. + return true; + } + + return false; + } + + /** + * Lock process + * + * Lock the process so that multiple instances can't run simultaneously. + * Override if applicable, but the duration should be greater than that + * defined in the time_exceeded() method. + */ + protected function lock_process() { + $this->start_time = time(); // Set start time of current process. + + $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute + $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); + + set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); + } + + /** + * Unlock process + * + * Unlock the process so that other instances can spawn. + * + * @return $this + */ + protected function unlock_process() { + delete_site_transient( $this->identifier . '_process_lock' ); + + return $this; + } + + /** + * Get batch + * + * @return stdClass Return the first batch from the queue + */ + protected function get_batch() { + global $wpdb; + + $table = $wpdb->options; + $column = 'option_name'; + $key_column = 'option_id'; + $value_column = 'option_value'; + + if ( is_multisite() ) { + $table = $wpdb->sitemeta; + $column = 'meta_key'; + $key_column = 'meta_id'; + $value_column = 'meta_value'; + } + + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; + + $query = $wpdb->get_row( $wpdb->prepare( " + SELECT * + FROM {$table} + WHERE {$column} LIKE %s + ORDER BY {$key_column} ASC + LIMIT 1 + ", $key ) ); + + $batch = new stdClass(); + $batch->key = $query->$column; + $batch->data = maybe_unserialize( $query->$value_column ); + + return $batch; + } + + /** + * Handle + * + * Pass each queue item to the task handler, while remaining + * within server memory and time limit constraints. + */ + protected function handle() { + $this->lock_process(); + + do { + $batch = $this->get_batch(); + + foreach ( $batch->data as $key => $value ) { + $task = $this->task( $value ); + + if ( false !== $task ) { + $batch->data[ $key ] = $task; + } else { + unset( $batch->data[ $key ] ); + } + + if ( $this->time_exceeded() || $this->memory_exceeded() ) { + // Batch limits reached. + break; + } + } + + // Update or delete current batch. + if ( ! empty( $batch->data ) ) { + $this->update( $batch->key, $batch->data ); + } else { + $this->delete( $batch->key ); + } + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); + + $this->unlock_process(); + + // Start next batch or complete process. + if ( ! $this->is_queue_empty() ) { + $this->dispatch(); + } else { + $this->complete(); + } + + wp_die(); + } + + /** + * Memory exceeded + * + * Ensures the batch process never exceeds 90% + * of the maximum WordPress memory. + * + * @return bool + */ + protected function memory_exceeded() { + $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory + $current_memory = memory_get_usage( true ); + $return = false; + + if ( $current_memory >= $memory_limit ) { + $return = true; + } + + return apply_filters( $this->identifier . '_memory_exceeded', $return ); + } + + /** + * Get memory limit + * + * @return int + */ + protected function get_memory_limit() { + if ( function_exists( 'ini_get' ) ) { + $memory_limit = ini_get( 'memory_limit' ); + } else { + // Sensible default. + $memory_limit = '128M'; + } + + if ( ! $memory_limit || -1 === intval( $memory_limit ) ) { + // Unlimited, set to 32GB. + $memory_limit = '32000M'; + } + + return intval( $memory_limit ) * 1024 * 1024; + } + + /** + * Time exceeded. + * + * Ensures the batch never exceeds a sensible time limit. + * A timeout limit of 30s is common on shared hosting. + * + * @return bool + */ + protected function time_exceeded() { + $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds + $return = false; + + if ( time() >= $finish ) { + $return = true; + } + + return apply_filters( $this->identifier . '_time_exceeded', $return ); + } + + /** + * Complete. + * + * Override if applicable, but ensure that the below actions are + * performed, or, call parent::complete(). + */ + protected function complete() { + // Unschedule the cron healthcheck. + $this->clear_scheduled_event(); + } + + /** + * Schedule cron healthcheck + * + * @access public + * @param mixed $schedules Schedules. + * @return mixed + */ + public function schedule_cron_healthcheck( $schedules ) { + $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); + + if ( property_exists( $this, 'cron_interval' ) ) { + $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); + } + + // Adds every 5 minutes to the existing schedules. + $schedules[ $this->identifier . '_cron_interval' ] = array( + 'interval' => MINUTE_IN_SECONDS * $interval, + 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), + ); + + return $schedules; + } + + /** + * Handle cron healthcheck + * + * Restart the background process if not already running + * and data exists in the queue. + */ + public function handle_cron_healthcheck() { + if ( $this->is_process_running() ) { + // Background process already running. + exit; + } + + if ( $this->is_queue_empty() ) { + // No data to process. + $this->clear_scheduled_event(); + exit; + } + + $this->handle(); + + exit; + } + + /** + * Schedule event + */ + protected function schedule_event() { + if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { + wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); + } + } + + /** + * Clear scheduled event + */ + protected function clear_scheduled_event() { + $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); + + if ( $timestamp ) { + wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); + } + } + + /** + * Cancel Process + * + * Stop processing queue items, clear cronjob and delete batch. + * + */ + public function cancel_process() { + if ( ! $this->is_queue_empty() ) { + $batch = $this->get_batch(); + + $this->delete( $batch->key ); + + wp_clear_scheduled_hook( $this->cron_hook_identifier ); + } + + } + + /** + * Task + * + * Override this method to perform any actions required on each + * queue item. Return the modified item for further processing + * in the next pass through. Or, return false to remove the + * item from the queue. + * + * @param mixed $item Queue item to iterate over. + * + * @return mixed + */ + abstract protected function task( $item ); + + } +} diff --git a/dist/trunk/vendor/a5hleyrich/wp-background-processing/composer.json b/dist/trunk/vendor/a5hleyrich/wp-background-processing/composer.json new file mode 100644 index 0000000..42956d6 --- /dev/null +++ b/dist/trunk/vendor/a5hleyrich/wp-background-processing/composer.json @@ -0,0 +1,18 @@ +{ + "name": "a5hleyrich/wp-background-processing", + "description": "WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks.", + "type": "library", + "require": { + "php": ">=5.2" + }, + "license": "GPL-2.0-only", + "authors": [ + { + "name": "Ashley Rich", + "email": "hello@ashleyrich.com" + } + ], + "autoload": { + "classmap": [ "classes/" ] + } +} \ No newline at end of file diff --git a/dist/trunk/vendor/a5hleyrich/wp-background-processing/license.txt b/dist/trunk/vendor/a5hleyrich/wp-background-processing/license.txt new file mode 100644 index 0000000..a0939e9 --- /dev/null +++ b/dist/trunk/vendor/a5hleyrich/wp-background-processing/license.txt @@ -0,0 +1,280 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110, USA + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/dist/trunk/vendor/a5hleyrich/wp-background-processing/wp-background-processing.php b/dist/trunk/vendor/a5hleyrich/wp-background-processing/wp-background-processing.php new file mode 100644 index 0000000..2205562 --- /dev/null +++ b/dist/trunk/vendor/a5hleyrich/wp-background-processing/wp-background-processing.php @@ -0,0 +1,20 @@ + $vendorDir . '/composer/installers/src/Composer/Installers/ZendInstaller.php', 'Composer\\Installers\\ZikulaInstaller' => $vendorDir . '/composer/installers/src/Composer/Installers/ZikulaInstaller.php', 'Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin' => $vendorDir . '/dealerdirect/phpcodesniffer-composer-installer/src/Plugin.php', + 'WP_Async_Request' => $vendorDir . '/a5hleyrich/wp-background-processing/classes/wp-async-request.php', + 'WP_Background_Process' => $vendorDir . '/a5hleyrich/wp-background-processing/classes/wp-background-process.php', 'croox\\wde\\Block' => $vendorDir . '/croox/wp-dev-env-frame/src/Block.php', 'croox\\wde\\Childtheme' => $vendorDir . '/croox/wp-dev-env-frame/src/Childtheme.php', 'croox\\wde\\Childtheme_Enfold' => $vendorDir . '/croox/wp-dev-env-frame/src/Childtheme_Enfold.php', @@ -116,6 +118,7 @@ 'croox\\wde\\utils\\Post' => $vendorDir . '/croox/wp-dev-env-frame/src/utils/Post.php', 'croox\\wde\\utils\\Post_Type' => $vendorDir . '/croox/wp-dev-env-frame/src/utils/Post_Type.php', 'croox\\wde\\utils\\Wpml' => $vendorDir . '/croox/wp-dev-env-frame/src/utils/Wpml.php', + 'yaim\\Admin_Message_Log' => $baseDir . '/classes/Admin_Message_Log.php', 'yaim\\Import_Base' => $baseDir . '/classes/Import_Base.php', 'yaim\\Import_Posts' => $baseDir . '/classes/Import_Posts.php', 'yaim\\Import_Terms' => $baseDir . '/classes/Import_Terms.php', diff --git a/dist/trunk/vendor/composer/autoload_static.php b/dist/trunk/vendor/composer/autoload_static.php index 27608c2..17f4980 100644 --- a/dist/trunk/vendor/composer/autoload_static.php +++ b/dist/trunk/vendor/composer/autoload_static.php @@ -144,6 +144,8 @@ class ComposerStaticInit4175d1eef81cb139c295a9a6bbc01838 'Composer\\Installers\\ZendInstaller' => __DIR__ . '/..' . '/composer/installers/src/Composer/Installers/ZendInstaller.php', 'Composer\\Installers\\ZikulaInstaller' => __DIR__ . '/..' . '/composer/installers/src/Composer/Installers/ZikulaInstaller.php', 'Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin' => __DIR__ . '/..' . '/dealerdirect/phpcodesniffer-composer-installer/src/Plugin.php', + 'WP_Async_Request' => __DIR__ . '/..' . '/a5hleyrich/wp-background-processing/classes/wp-async-request.php', + 'WP_Background_Process' => __DIR__ . '/..' . '/a5hleyrich/wp-background-processing/classes/wp-background-process.php', 'croox\\wde\\Block' => __DIR__ . '/..' . '/croox/wp-dev-env-frame/src/Block.php', 'croox\\wde\\Childtheme' => __DIR__ . '/..' . '/croox/wp-dev-env-frame/src/Childtheme.php', 'croox\\wde\\Childtheme_Enfold' => __DIR__ . '/..' . '/croox/wp-dev-env-frame/src/Childtheme_Enfold.php', @@ -159,6 +161,7 @@ class ComposerStaticInit4175d1eef81cb139c295a9a6bbc01838 'croox\\wde\\utils\\Post' => __DIR__ . '/..' . '/croox/wp-dev-env-frame/src/utils/Post.php', 'croox\\wde\\utils\\Post_Type' => __DIR__ . '/..' . '/croox/wp-dev-env-frame/src/utils/Post_Type.php', 'croox\\wde\\utils\\Wpml' => __DIR__ . '/..' . '/croox/wp-dev-env-frame/src/utils/Wpml.php', + 'yaim\\Admin_Message_Log' => __DIR__ . '/../..' . '/classes/Admin_Message_Log.php', 'yaim\\Import_Base' => __DIR__ . '/../..' . '/classes/Import_Base.php', 'yaim\\Import_Posts' => __DIR__ . '/../..' . '/classes/Import_Posts.php', 'yaim\\Import_Terms' => __DIR__ . '/../..' . '/classes/Import_Terms.php', diff --git a/dist/trunk/vendor/composer/installed.json b/dist/trunk/vendor/composer/installed.json index 623addc..c001bb9 100644 --- a/dist/trunk/vendor/composer/installed.json +++ b/dist/trunk/vendor/composer/installed.json @@ -672,5 +672,43 @@ "yaml", "yml" ] + }, + { + "name": "a5hleyrich/wp-background-processing", + "version": "1.0.1", + "version_normalized": "1.0.1.0", + "source": { + "type": "git", + "url": "https://github.com/A5hleyRich/wp-background-processing.git", + "reference": "1f070aab5058dbaf45d5435a343033ddd8a641b1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/A5hleyRich/wp-background-processing/zipball/1f070aab5058dbaf45d5435a343033ddd8a641b1", + "reference": "1f070aab5058dbaf45d5435a343033ddd8a641b1", + "shasum": "" + }, + "require": { + "php": ">=5.2" + }, + "time": "2018-02-12 09:24:05", + "type": "library", + "installation-source": "dist", + "autoload": { + "classmap": [ + "classes/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-only" + ], + "authors": [ + { + "name": "Ashley Rich", + "email": "hello@ashleyrich.com" + } + ], + "description": "WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks." } ] diff --git a/dist/trunk/yaml-importer.php b/dist/trunk/yaml-importer.php index e0762c4..472edfc 100644 --- a/dist/trunk/yaml-importer.php +++ b/dist/trunk/yaml-importer.php @@ -3,7 +3,7 @@ Plugin Name: YAML Importer Plugin URI: https://github.com/croox/yaml-importer Description: Import Posts from YAML - Version: 0.0.2 + Version: 0.1.0 Author: croox Author URI: https://github.com/croox License: GNU General Public License v2 or later @@ -32,7 +32,7 @@ function yaim_init() { $init_args = array( - 'version' => '0.0.2', + 'version' => '0.1.0', 'slug' => 'yaml-importer', 'name' => 'YAML Importer', 'prefix' => 'yaim', diff --git a/package.json b/package.json index b045c0a..ed261bb 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "uri": "https://github.com/croox/yaml-importer", "textDomain": "yaim", "domainPath": "/languages", - "version": "0.0.2", + "version": "0.1.0", "description": "Import Posts from YAML", "main": "index.js", "author": "croox", diff --git a/src/languages/yaim-LOCALE.pot b/src/languages/yaim-LOCALE.pot index 6633132..512b020 100644 --- a/src/languages/yaim-LOCALE.pot +++ b/src/languages/yaim-LOCALE.pot @@ -6,9 +6,9 @@ #, fuzzy msgid "" msgstr "" -"Project-Id-Version: yaml-importer 0.0.2\n" +"Project-Id-Version: yaml-importer 0.1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-09-23 12:34+0545\n" +"POT-Creation-Date: 2019-09-25 19:35+0545\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,16 +17,30 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" -#: src/classes/Import_Base.php:104 +#: src/classes/Import_Base.php:162 #, php-format msgid "\"%s\" is not translatable." msgstr "" -#: src/classes/Settings_Page.php:28 +#: src/classes/Import_Base.php:220 #, php-format -msgid "Argument \"%s\" missing" +msgid "\"%s\" has translated and non-translated input." msgstr "" -#: src/classes/Settings_Page.php:51 +#: src/classes/Settings_Page.php:58 msgid "YAML Importer" msgstr "" + +#. /add_submenu_page(). +#. +#. The option key and admin menu page slug. +#. 'icon_url' => 'dashicons-download', // Menu icon. Only applicable if 'parent_slug' is left empty. +#. 'menu_title' => esc_html__( 'Options', 'yaim' ), // Falls back to 'title' (above). +#. Make options page a submenu item of the themes menu. +#. 'capability' => 'manage_options', // Cap required to view options-page. +#. 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty. +#. 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page. +#. 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()). +#: src/classes/Settings_Page.php:74 +msgid "Start Import" +msgstr "" diff --git a/yaml-importer.php b/yaml-importer.php index c745a0d..0c35ed3 100644 --- a/yaml-importer.php +++ b/yaml-importer.php @@ -3,7 +3,7 @@ Plugin Name: YAML Importer Plugin URI: https://github.com/croox/yaml-importer Description: Import Posts from YAML - Version: 0.0.2 + Version: 0.1.0 Author: croox Author URI: https://github.com/croox License: GNU General Public License v2 or later