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 |