| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Sync; |
| 4 |
|
| 5 |
use SureCart\Models\Concerns\Facade; |
| 6 |
|
| 7 |
/** |
| 8 |
* This class syncs product records to WordPress posts. |
| 9 |
*/ |
| 10 |
class CollectionSyncService { |
| 11 |
use Facade; |
| 12 |
|
| 13 |
/** |
| 14 |
* The post. |
| 15 |
* |
| 16 |
* @var \WP_Post |
| 17 |
*/ |
| 18 |
protected $term; |
| 19 |
|
| 20 |
/** |
| 21 |
* The post type. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $taxonomy = 'sc_collection'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Find the post from the model. |
| 29 |
* |
| 30 |
* @param string $model_id The model id. |
| 31 |
* |
| 32 |
* @return \WP_Post|\WP_Error|null |
| 33 |
*/ |
| 34 |
protected function findByModelId( string $model_id ) { |
| 35 |
// if we don't have a model id, return null. |
| 36 |
if ( empty( $model_id ) ) { |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
// get term by sc_id. |
| 41 |
$terms = get_terms( |
| 42 |
array( |
| 43 |
'taxonomy' => $this->taxonomy, |
| 44 |
'meta_key' => 'sc_id', |
| 45 |
'meta_value' => $model_id, |
| 46 |
'number' => 1, |
| 47 |
'hide_empty' => false, |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
// handle error. |
| 52 |
if ( is_wp_error( $terms ) ) { |
| 53 |
return $terms; |
| 54 |
} |
| 55 |
|
| 56 |
// return the post. |
| 57 |
return apply_filters( 'surecart_get_collection_term', $terms[0] ?? null, $model_id, $this ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Sync the model with the post. |
| 62 |
* |
| 63 |
* @param \SureCart\Models\ProductCollection $model The model. |
| 64 |
* |
| 65 |
* @return \WP_Post|\WP_Error |
| 66 |
*/ |
| 67 |
protected function sync( \SureCart\Models\ProductCollection $model ) { |
| 68 |
$this->term = $this->findByModelId( $model->id ); |
| 69 |
|
| 70 |
if ( is_wp_error( $this->term ) ) { |
| 71 |
return $this->term; |
| 72 |
} |
| 73 |
|
| 74 |
return empty( $this->term->term_id ) ? $this->create( $model ) : $this->update( $model ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Delete the synced post. |
| 79 |
* |
| 80 |
* @param string $id The model id. |
| 81 |
* |
| 82 |
* @return \WP_Post|\WP_Error|false|null |
| 83 |
*/ |
| 84 |
protected function delete( string $id ) { |
| 85 |
$this->term = $this->findByModelId( $id ); |
| 86 |
|
| 87 |
if ( is_wp_error( $this->term ) ) { |
| 88 |
return $this->term; |
| 89 |
} |
| 90 |
|
| 91 |
// force delete post. |
| 92 |
return wp_delete_term( $this->term->term_id, $this->taxonomy ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Create the term. |
| 97 |
* |
| 98 |
* @param \SureCart\Models\ProductCollection $collection The collection model. |
| 99 |
* |
| 100 |
* @return \WP_Post|\WP_Error |
| 101 |
*/ |
| 102 |
protected function create( \SureCart\Models\ProductCollection $collection ) { |
| 103 |
// don't do these actions as they can slow down the sync. |
| 104 |
foreach ( [ 'do_pings', 'transition_post_status', 'save_post', 'pre_post_update', 'add_attachment', 'edit_attachment', 'edit_post', 'term_updated', 'wp_insert_term' ] as $action ) { |
| 105 |
remove_all_actions( $action ); |
| 106 |
} |
| 107 |
|
| 108 |
// we are importing. |
| 109 |
if ( ! defined( 'WP_IMPORTING' ) ) { |
| 110 |
define( 'WP_IMPORTING', true ); |
| 111 |
} |
| 112 |
|
| 113 |
$term = wp_insert_term( |
| 114 |
$collection->name, |
| 115 |
$this->taxonomy, |
| 116 |
[ |
| 117 |
'name' => $collection->name, |
| 118 |
'description' => $collection->description, |
| 119 |
'slug' => $collection->slug, |
| 120 |
] |
| 121 |
); |
| 122 |
|
| 123 |
// handle errors. |
| 124 |
if ( is_wp_error( $term ) ) { |
| 125 |
return $term; |
| 126 |
} |
| 127 |
|
| 128 |
$this->term = get_term( $term['term_id'], $this->taxonomy ); |
| 129 |
|
| 130 |
if ( is_wp_error( $this->term ) || empty( $this->term ) ) { |
| 131 |
return $this->term; |
| 132 |
} |
| 133 |
|
| 134 |
update_term_meta( $this->term->term_id, 'sc_account', \SureCart::account()->id ); |
| 135 |
update_term_meta( $this->term->term_id, 'sc_id', $collection->id ); |
| 136 |
update_term_meta( $this->term->term_id, 'collection', $collection->toArray() ); |
| 137 |
|
| 138 |
return $this->term; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Update the term. |
| 143 |
* |
| 144 |
* @param \SureCart\Models\ProductCollection $collection The collection model. |
| 145 |
* |
| 146 |
* @return \WP_Post|\WP_Error |
| 147 |
*/ |
| 148 |
protected function update( \SureCart\Models\ProductCollection $collection ) { |
| 149 |
// find the post. |
| 150 |
if ( empty( $this->term ) || ! is_a( $this->term, 'WP_Term' ) ) { |
| 151 |
$this->term = $this->findByModelId( $collection->id ); |
| 152 |
} |
| 153 |
|
| 154 |
$term = wp_update_term( |
| 155 |
$this->term->term_id, |
| 156 |
$this->taxonomy, |
| 157 |
[ |
| 158 |
'name' => $collection->name, |
| 159 |
'description' => $collection->description, |
| 160 |
'slug' => $collection->slug, |
| 161 |
] |
| 162 |
); |
| 163 |
|
| 164 |
// handle errors. |
| 165 |
if ( is_wp_error( $term ) ) { |
| 166 |
return $term; |
| 167 |
} |
| 168 |
|
| 169 |
update_term_meta( $term['term_id'], 'sc_account', \SureCart::account()->id ); |
| 170 |
update_term_meta( $term['term_id'], 'sc_id', $collection->id ); |
| 171 |
update_term_meta( $term['term_id'], 'collection', $collection->toArray() ); |
| 172 |
|
| 173 |
$this->term = get_term( $term['term_id'], $this->taxonomy ); |
| 174 |
|
| 175 |
return $this->term; |
| 176 |
} |
| 177 |
} |
| 178 |
|