| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Interactions\Cache; |
| 4 |
|
| 5 |
use Elementor\Core\Base\Document; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Interactions_Postmeta { |
| 12 |
const META_KEY = 'elementor-interactions-cache'; |
| 13 |
|
| 14 |
public function load_content( $post_id ) { |
| 15 |
return get_post_meta( $post_id, self::META_KEY, true ); |
| 16 |
} |
| 17 |
|
| 18 |
public function process_content( $post_id, $data ) { |
| 19 |
if ( $this->skip_processing( $data ) ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$elements_interactions = $this->extract_elements_interactions( $data ); |
| 24 |
|
| 25 |
$this->save_postmeta( $post_id, $elements_interactions ); |
| 26 |
|
| 27 |
return $elements_interactions; |
| 28 |
} |
| 29 |
|
| 30 |
private function save_postmeta( $post_id, array $interactions ) { |
| 31 |
if ( empty( $interactions ) ) { |
| 32 |
delete_post_meta( $post_id, self::META_KEY ); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
update_post_meta( $post_id, self::META_KEY, $interactions ); |
| 37 |
} |
| 38 |
|
| 39 |
private function skip_processing( array $data ) { |
| 40 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
if ( isset( $data['settings']['post_status'] ) && Document::STATUS_AUTOSAVE === $data['settings']['post_status'] ) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
private function extract_elements_interactions( array $data ) { |
| 52 |
$parser = new Elements_Interactions(); |
| 53 |
$parser->parse_from( $data ); |
| 54 |
return $parser->all(); |
| 55 |
} |
| 56 |
} |
| 57 |
|