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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / compatibility / Sensei / class-wcml-sensei.php
woocommerce-multilingual / compatibility / Sensei Last commit date
Factory.php 4 weeks ago class-wcml-sensei.php 4 weeks ago
class-wcml-sensei.php
228 lines
1 <?php
2
3 class WCML_Sensei implements \IWPML_Action {
4
5 private $sitepress;
6 private $custom_columns;
7
8 public function __construct( SitePress $sitepress, WPML_Custom_Columns $custom_columns ) {
9 $this->sitepress = $sitepress;
10 $this->custom_columns = $custom_columns;
11 }
12
13 public function add_hooks() {
14
15 add_filter( 'manage_edit-lesson_columns', [ $this->custom_columns, 'add_posts_management_column' ] );
16 add_filter( 'manage_edit-course_columns', [ $this->custom_columns, 'add_posts_management_column' ] );
17 add_filter( 'manage_edit-question_columns', [ $this->custom_columns, 'add_posts_management_column' ] );
18
19 add_action( 'save_post', [ $this, 'save_post_actions' ], 100, 2 );
20 add_action( 'sensei_log_activity_after', [ $this, 'log_activity_after' ], 10, 3 );
21 add_filter( 'sensei_bought_product_id', [ $this, 'filter_bought_product_id' ], 10, 2 );
22 add_action( 'delete_comment', [ $this, 'delete_user_activity' ] );
23
24 add_action( 'pre_get_comments', [ $this, 'pre_get_comments' ] );
25
26 if ( $this->is_sensei_admin_without_language_switcher() ) {
27 remove_action( 'wp_before_admin_bar_render', [ $this->sitepress, 'admin_language_switcher' ] );
28 }
29 }
30
31 private function is_sensei_admin_without_language_switcher() {
32
33 $is_message_post_type = isset( $_GET['post_type'] ) && $_GET['post_type'] == 'sensei_message';
34 $is_grading_page = isset( $_GET['page'] ) && $_GET['page'] == 'sensei_grading';
35
36 return is_admin() && ( $is_message_post_type || $is_grading_page );
37 }
38
39 public function save_post_actions( $post_id, $post ) {
40 global $sitepress;
41
42 if ( ! in_array( $post->post_type, [ 'lesson', 'course', 'quiz' ] ) ) {
43 return;
44 }
45 if ( $post->post_status == 'auto-draft' ) {
46 return;
47 }
48 if ( isset( $_POST['autosave'] ) ) {
49 return;
50 }
51
52 if ( $post->post_type == 'quiz' && isset( $_POST['ID'] ) ) {
53 $this->save_post_actions( $_POST['ID'], get_post( $_POST['ID'] ) );
54 }
55
56 $trid = $sitepress->get_element_trid( $post_id, 'post_' . $post->post_type );
57 $translations = $sitepress->get_element_translations( $trid, 'post_' . $post->post_type );
58
59 if ( ! empty( $translations ) ) {
60 $original_post_id = false;
61 foreach ( $translations as $t ) {
62 if ( $t->original ) {
63 $original_post_id = $t->element_id;
64 break;
65 }
66 }
67
68 if ( $post_id != $original_post_id ) {
69 $this->sync_custom_fields( $original_post_id, $post_id, $post->post_type );
70 } else {
71 foreach ( $translations as $t ) {
72 if ( $original_post_id != $t->element_id ) {
73 $this->sync_custom_fields( $original_post_id, $t->element_id, $post->post_type );
74 }
75 }
76 }
77 }
78
79 }
80
81
82 public function sync_custom_fields( $original_post_id, $post_id, $post_type ) {
83 global $sitepress;
84
85 $language = $sitepress->get_language_for_element( $post_id, 'post_' . $post_type );
86 if ( $post_type == 'quiz' ) {
87
88 $lesson_id = get_post_meta( $original_post_id, '_quiz_lesson', true );
89
90 if ( $lesson_id ) {
91 $tr_lesson_id = apply_filters( 'wpml_object_id', $lesson_id, 'lesson', false, $language );
92
93 if ( ! is_null( $tr_lesson_id ) ) {
94 update_post_meta( $post_id, '_quiz_lesson', $tr_lesson_id );
95 }
96 } else {
97 delete_post_meta( $post_id, '_quiz_lesson' );
98 }
99 } elseif ( $post_type == 'lesson' ) {
100 $course_id = get_post_meta( $original_post_id, '_lesson_course', true );
101
102 if ( $course_id ) {
103 $tr_course_id = apply_filters( 'wpml_object_id', $course_id, 'course', false, $language );
104
105 if ( ! is_null( $tr_course_id ) ) {
106 update_post_meta( $post_id, '_lesson_course', $tr_course_id );
107 }
108 } else {
109 delete_post_meta( $post_id, '_lesson_course' );
110 }
111
112 $lesson_id = get_post_meta( $original_post_id, '_lesson_prerequisite', true );
113
114 if ( $lesson_id ) {
115 $tr_lesson_id = apply_filters( 'wpml_object_id', $lesson_id, 'lesson', false, $language );
116
117 if ( ! is_null( $tr_lesson_id ) ) {
118 update_post_meta( $post_id, '_lesson_prerequisite', $tr_lesson_id );
119 }
120 } else {
121 delete_post_meta( $post_id, '_lesson_prerequisite' );
122 }
123 } else {
124
125 $product_id = get_post_meta( $original_post_id, '_course_woocommerce_product', true );
126
127 if ( $product_id ) {
128 $tr_product_id = apply_filters( 'wpml_object_id', $product_id, get_post_type( $product_id ), false, $language );
129
130 if ( ! is_null( $tr_product_id ) ) {
131 update_post_meta( $post_id, '_course_woocommerce_product', $tr_product_id );
132 }
133 } else {
134 delete_post_meta( $post_id, '_course_woocommerce_product' );
135 }
136
137 $course_id = get_post_meta( $original_post_id, '_course_prerequisite', true );
138
139 if ( $course_id ) {
140 $tr_course_id = apply_filters( 'wpml_object_id', $course_id, 'course', false, $language );
141
142 if ( ! is_null( $tr_course_id ) ) {
143 update_post_meta( $post_id, '_course_prerequisite', $tr_course_id );
144 }
145 } else {
146 delete_post_meta( $post_id, '_course_prerequisite' );
147 }
148 }
149
150 }
151
152 public function log_activity_after( $args, $data, $comment_id = false ) {
153 global $sitepress;
154
155 if ( ! $comment_id ) {
156 return false;
157 }
158
159 $comment_post_id = $data['comment_post_ID'];
160 $trid = $sitepress->get_element_trid( $comment_post_id, 'post_' . get_post_type( $comment_post_id ) );
161 $translations = $sitepress->get_element_translations( $trid, 'post_' . get_post_type( $comment_post_id ) );
162
163 foreach ( $translations as $translation ) {
164
165 if ( $comment_post_id != $translation->element_id ) {
166 $data['comment_post_ID'] = $translation->element_id;
167
168 $trid = $sitepress->get_element_trid( $comment_id, 'comment' );
169
170 $tr_comment_id = apply_filters( 'wpml_object_id', $comment_id, 'comment', false, $translation->language_code );
171 if ( isset( $args['action'] ) && 'update' == $args['action'] && ! is_null( $tr_comment_id ) && get_comment( $tr_comment_id ) ) {
172 $data['comment_ID'] = $tr_comment_id;
173 $tr_comment_id = wp_update_comment( $data );
174 } else {
175 $tr_comment_id = wp_insert_comment( $data );
176 $sitepress->set_element_language_details( $tr_comment_id, 'comment', $trid, $translation->language_code );
177 }
178 }
179 }
180
181 }
182
183 public function filter_bought_product_id( $product_id, $order ) {
184 $order_id = method_exists( 'WC_Order', 'get_id' ) ? $order->get_id() : $order->id;
185 $order_language = WCML_Orders::getLanguage( $order_id );
186
187 $tr_product_id = apply_filters( 'wpml_object_id', $product_id, get_post_type( $product_id ), false, $order_language );
188
189 if ( ! is_null( $tr_product_id ) ) {
190 return $tr_product_id;
191 } else {
192 return $product_id;
193 }
194 }
195
196 public function delete_user_activity( $comment_id ) {
197 global $sitepress;
198
199 $comment_type = get_comment_type( $comment_id );
200
201 if ( strstr( $comment_type, 'sensei' ) ) {
202
203 $trid = $sitepress->get_element_trid( $comment_id, 'comment' );
204 $translations = $sitepress->get_element_translations( $trid, 'comment' );
205
206 remove_action( 'delete_comment', [ $this, 'delete_user_activity' ] );
207 foreach ( $translations as $translation ) {
208 if ( $comment_id != $translation->element_id ) {
209 wp_delete_comment( $translation->element_id, true );
210 }
211 }
212 }
213
214 }
215
216 public function pre_get_comments( $obj ) {
217 global $sitepress;
218
219 if ( $obj->query_vars['type'] == 'sensei_course_start' ) {
220
221 remove_filter( 'comments_clauses', [ $sitepress, 'comments_clauses' ], 10 );
222
223 }
224
225 }
226
227 }
228