PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.2
YayMail – WooCommerce Email Customizer v4.4.2
4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 3.4.2 3.4.3 All 55 releases
yaymail / src / Models / RevisionModel.php

RevisionModel.php in YayMail – WooCommerce Email Customizer 4.4.2, at src/Models/RevisionModel.php

120 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Models;
4
5 use YayMail\Utils\SingletonTrait;
6 use YayMail\YayMailTemplate;
7
8 /**
9 * Revision Model
10 *
11 * @method static RevisionModel get_instance()
12 */
13 class RevisionModel {
14
15 use SingletonTrait;
16
17 private $meta_keys = YayMailTemplate::META_KEYS;
18
19 /**
20 * Save revision of updated template
21 *
22 * @param int $post_id ID of the updated template
23 * @param array $updated_data An array containing the updated data fields and values
24 *
25 * @return void
26 */
27 public function save( $post_id, $updated_data ) {
28 $all_revisions = array_values(
29 wp_get_post_revisions(
30 $post_id,
31 [
32 'order' => 'DESC',
33 'numberposts' => 1,
34 ]
35 )
36 );
37 $latest_revision = $all_revisions[0] ?? null;
38
39 if ( empty( $latest_revision ) ) {
40 return;
41 }
42
43 $revision_post_id = $latest_revision->ID;
44 if ( ! $revision_post_id ) {
45 return null;
46 }
47
48 /**
49 * Update the post_modified as it was copied from parent
50 */
51 $current_datetime = current_time( 'mysql' );
52 wp_update_post(
53 [
54 'ID' => $revision_post_id,
55 'post_modified' => $current_datetime,
56 'post_modified_gmt' => get_gmt_from_date( $current_datetime ),
57 ]
58 );
59
60 $updated_data['modified_by'] = wp_get_current_user()->data->display_name;
61 foreach ( $updated_data as $key => $value ) {
62 if ( isset( $this->meta_keys[ $key ] ) ) {
63 update_metadata( 'post', $revision_post_id, $this->meta_keys[ $key ], $value );
64 }
65 }
66
67 return [
68 'revision_id' => $revision_post_id,
69 'modified_by' => get_post_meta( $revision_post_id, $this->meta_keys['modified_by'], true ),
70 'modified_at' => get_post( $revision_post_id )->post_modified,
71 'template_elements' => isset( $updated_data['elements'] ) ? $updated_data['elements'] : [],
72 'background_color' => isset( $updated_data['background_color'] ) ? $updated_data['background_color'] : '',
73 'text_link_color' => isset( $updated_data['text_link_color'] ) ? $updated_data['text_link_color'] : '',
74 ];
75 }
76
77 public function get_by_template( $template_name ) {
78 $template_data = TemplateModel::find_by_name( $template_name );
79 if ( empty( $template_data['id'] ) ) {
80 return [];
81 }
82 $revisions = wp_get_post_revisions( $template_data['id'] );
83 $result = [];
84
85 foreach ( $revisions as $revision ) {
86 $result[] = array_merge(
87 [
88 'revision_id' => $revision->ID,
89 'modified_by' => get_post_meta( $revision->ID, $this->meta_keys['modified_by'], true ),
90 'modified_at' => $revision->post_modified,
91 ],
92 $this->get_by_id( $revision->ID )
93 );
94 }
95
96 return $result;
97 }
98 public function get_by_id( $revision_id ) {
99 $result = [];
100 $result['template_elements'] = get_post_meta( $revision_id, $this->meta_keys['elements'], true );
101 $result['background_color'] = get_post_meta( $revision_id, $this->meta_keys['background_color'], true );
102 $result['text_link_color'] = get_post_meta( $revision_id, $this->meta_keys['text_link_color'], true );
103 return $result;
104 }
105
106 public function delete_by_template( $template_name ) {
107 $template_data = TemplateModel::find_by_name( $template_name );
108 if ( empty( $template_data['id'] ) ) {
109 return [
110 'success' => false,
111 ];
112 }
113 $revisions = wp_get_post_revisions( $template_data['id'] );
114
115 foreach ( $revisions as $revision ) {
116 wp_delete_post_revision( $revision );
117 }
118 }
119 }
120