public function customer_note_added($data) { global $wpdb; $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE id = %d", $data['order_id'])); if ($post && $post->post_type == 'order_shipment') { $parent_id = $post->post_parent; $is_customer_note = intval(1); if (isset($_SERVER['HTTP_HOST'])) { $comment_author_email = sanitize_email(strtolower(__('WooCommerce', 'woocommerce')) . '@' . str_replace('www.', '', $_SERVER['HTTP_HOST'])); } else { $comment_author_email = sanitize_email(strtolower(__('WooCommerce', 'woocommerce')) . '@noreply.com'); } $comment_post_ID = $parent_id; $comment_author = __('WooCommerce', 'woocommerce'); $comment_author_url = ''; $comment_content = $data['customer_note']; $comment_agent = 'WooCommerce'; $comment_type = 'order_note'; $comment_parent = 0; $comment_approved = 1; $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_agent', 'comment_type', 'comment_parent', 'comment_approved'); $comment_id = wp_insert_comment($commentdata); add_comment_meta($comment_id, 'is_customer_note', $is_customer_note); } }
function custom_save_comment_wp($postID, $userID, $author, $email, $comment, $ratingvalue) { remove_all_actions('comment_post', 1); $_POST['crfp-rating'] = $ratingvalue; $commentdata = array('comment_post_ID' => $postID, 'comment_author' => $author, 'comment_author_email' => $email, 'comment_content' => $comment, 'comment_type' => '', 'comment_parent' => 0, 'user_id' => $userID); /*Graba el comentario y me da el ID*/ $commentID = wp_new_comment($commentdata); /*Añade el meta con el rating*/ add_comment_meta($commentID, 'crfp-rating', $ratingvalue, true); //add_comment_meta($commentID, 'crfp-rating', 4, true); /*Actualiza el total y el promedio del rating*/ $comments = get_comments(array('post_id' => $postID, 'status' => 'approve')); $totalRating = 0; $totalRatings = 0; $averageRating = 0; if (is_array($comments) and count($comments) > 0) { foreach ($comments as $comment) { $rating = get_comment_meta($comment->comment_ID, 'crfp-rating', true); if ($rating > 0) { $totalRatings++; $totalRating += $rating; } } $averageRating = ($totalRatings == 0 or $totalRating == 0) ? 0 : round($totalRating / $totalRatings, 0); } update_post_meta($postID, 'crfp-total-ratings', $totalRatings); update_post_meta($postID, 'crfp-average-rating', $averageRating); return true; }
/** * @ticket 21003 */ function test_orderby_meta() { $comment_id = $this->factory->comment->create(); $comment_id2 = $this->factory->comment->create(); $comment_id3 = $this->factory->comment->create(); add_comment_meta($comment_id, 'key', 'value1', true); add_comment_meta($comment_id, 'key1', 'value1', true); add_comment_meta($comment_id, 'key3', 'value3', true); add_comment_meta($comment_id2, 'key', 'value2', true); add_comment_meta($comment_id2, 'key2', 'value2', true); add_comment_meta($comment_id3, 'key3', 'value3', true); $comments = get_comments(array('meta_key' => 'key', 'orderby' => array('meta_value'))); $this->assertEquals(2, count($comments)); $this->assertEquals($comment_id2, $comments[0]->comment_ID); $this->assertEquals($comment_id, $comments[1]->comment_ID); $comments = get_comments(array('meta_key' => 'key', 'orderby' => array('meta_value'), 'order' => 'ASC')); $this->assertEquals(2, count($comments)); $this->assertEquals($comment_id, $comments[0]->comment_ID); $this->assertEquals($comment_id2, $comments[1]->comment_ID); $comments = get_comments(array('meta_value' => 'value3', 'orderby' => array('meta_value'))); $this->assertEquals(2, count($comments)); $this->assertEquals($comment_id, $comments[0]->comment_ID); $this->assertEquals($comment_id3, $comments[1]->comment_ID); // value1 is present on two different keys for $comment_id yet we should get only one instance // of that comment in the results $comments = get_comments(array('meta_value' => 'value1', 'orderby' => array('meta_value'))); $this->assertEquals(1, count($comments)); }
/** * Import comments from Instagram and load as WP comments for an image and post * * @param $access_token * @param $comments * @param $image_id * @param $post_id * @param $id * @param bool $sync */ public function import_comments($access_token, $comments, $image_id, $post_id, $id, $sync = false) { global $wpdb; if ($comments == '') { $comments = instagrate_pro()->accounts->get_comments($access_token, $image_id); $data = array('comments' => isset($comments) ? base64_encode(serialize($comments)) : array()); $where = array('id' => $id); $wpdb->update(instagrate_pro()->images->get_table_name(), $data, $where); } $meta_table = $wpdb->prefix . 'commentmeta'; if (!is_array($comments)) { return; } foreach ($comments as $comment) { $querystr = "\tSELECT count(*)\n\t\t\t\t\t\t\tFROM {$meta_table} m\n\t\t\t\t\t\t\tWHERE m.meta_key = '_igp_comment_id'\n\t\t\t\t\t\t\tAND m.meta_value = '{$comment->id}'\t"; $exists = $wpdb->get_var($querystr); if ($exists > 0) { continue; } // set comment data $data = array('comment_post_ID' => $post_id, 'comment_author' => $comment->from->username, 'comment_author_email' => '@instagram_igp', 'comment_author_url' => 'http://instagram.com/' . $comment->from->username, 'comment_content' => instagrate_pro()->helper->clean_caption($comment->text), 'comment_type' => '', 'comment_parent' => 0, 'user_id' => 0, 'comment_author_IP' => '127.0.0.1', 'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)', 'comment_date' => date('Y-m-d H:i:s', $comment->created_time), 'comment_approved' => Instagrate_Pro_Helper::setting('igpsettings_comments_auto-approve', '0')); $comment_id = wp_insert_comment($data); //set comment meta ig comment id add_comment_meta($comment_id, '_igp_comment_id', $comment->id, true); //set comment meta with user image url add_comment_meta($comment_id, '_igp_comment_avatar', $comment->from->profile_picture, true); } }
function import() { $comment_author = $this->author; $comment_author_url = $this->authoruri; $comment_author_email = $this->authoremail; $comment_date = $this->updated; $comment_content = $this->content; $comment_post_ID = $this->post_ID; $comment_author_IP = '127.0.0.1'; //Blogger does not supply the IP so default this // Clean up content // Simplepie does some cleaning but does not do these. $comment_content = str_replace('<br>', '<br />', $comment_content); $comment_content = str_replace('<hr>', '<hr />', $comment_content); $comment_parent = isset($this->parentcommentid) ? $this->parentcommentid : 0; $comment = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email','comment_author_IP','comment_date', 'comment_content', 'comment_parent'); $comment = wp_filter_comment($comment); $comment_id = wp_insert_comment($comment); //links of the form /feeds/417730729915399755/8397846992898424746/comments/default/7732208643735403000 add_comment_meta($comment_id, 'blogger_internal', $this->self, true); return $comment_id; }
function process_comments($post_id, $item_id) { if (empty($post_id)) { return; } if (!is_numeric($post_id)) { return; } $comments = $this->model->get_item_comments($item_id); if (!$comments || !isset($comments['data'])) { return; } $comments = $comments['data']; if (!count($comments)) { return false; } foreach ($comments as $comment) { if ($this->model->comment_already_imported($comment['id'])) { continue; } // We already have this comment, continue. $data = array('comment_post_ID' => $post_id, 'comment_date_gmt' => date('Y-m-d H:i:s', strtotime($comment['created_time'])), 'comment_author' => $comment['from']['name'], 'comment_author_url' => 'http://www.facebook.com/profile.php?id=' . $comment['from']['id'], 'comment_content' => $comment['message']); $meta = array('fb_comment_id' => $comment['id'], 'fb_author_id' => $comment['from']['id']); $data = wp_filter_comment($data); $comment_id = wp_insert_comment($data); add_comment_meta($comment_id, 'wdfb_comment', $meta); if ($this->model->data->get_option('wdfb_comments', 'notify_authors')) { wp_notify_postauthor($comment_id, 'comment'); } } }
function add_users_list_to_comment_for_notice($comment_id, $comment) { //get post ID and $post $post_id = $comment->comment_post_ID; $r = get_post($post_id); //it is cases? if ($r->post_type != 'cases') { return; } if ($comment->comment_type == 'visited') { return; } //add tag for plan email add_comment_meta($comment_id, 'email_notify', 0, true); //get members for cases $members = get_post_meta($post_id, 'members-cp-posts-sql'); //$message = $post_id . '<пост, участник: ' . print_r($members, true); //error_log($message); //add user id to list for notification foreach ($members as $member) { $id_usr = get_user_by_person($member); //Если участник и автор комментария есть одно и то же, то не нужно добавлять в список уведомлений (зачем автору читать свой же коммент на почте?) if ($comment->user_id == $id_usr) { continue; } //Если у участника есть пользователь if ($id_usr > 0) { add_comment_meta($comment_id, 'notify_user', $id_usr); //error_log('comment: '. $comment_id . ', val: ' . $user); } } return; }
function cwp_add_comment_meta_values($comment_id) { for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) { if (isset($_POST['meta_option_' . $i])) { ${'meta_option_' . $i} = wp_filter_nohtml_kses($_POST['meta_option_' . $i]); add_comment_meta($comment_id, 'meta_option_' . $i, ${'meta_option_' . $i}, false); } } }
/** * Rating field for comments. * * @param mixed $comment_id */ public static function add_comment_rating($comment_id) { if (isset($_POST['rating'])) { if (!$_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0) { return; } add_comment_meta($comment_id, 'rating', (int) esc_attr($_POST['rating']), true); } }
function update_comment($comment_id) { $comment = get_comment($comment_id); delete_comment_meta($comment_id, $this->meta_key); $terms = $this->update_comment_terms($comment_id, $comment); foreach ($terms as $term) { add_comment_meta($comment_id, $this->meta_key, $term); } $this->update_terms($comment->comment_post_ID); }
/** * Save the extra comment field values when the comment is posted. **/ function smittenkitten_save_comment_meta_data($comment_id) { if (isset($_POST['sk_madethis']) && '' != $_POST['sk_madethis']) { $sk_madethis = wp_filter_nohtml_kses($_POST['sk_madethis']); } add_comment_meta($comment_id, 'sk_madethis', $sk_madethis); if (isset($_POST['sk_question']) && '' != $_POST['sk_question']) { $sk_question = wp_filter_nohtml_kses($_POST['sk_question']); } add_comment_meta($comment_id, 'sk_question', $sk_question); }
/** * @param Rating $rating * * @return bool */ public function save_rating(Rating $rating) { // save rating $id = wp_insert_comment($rating->to_comment()); if ($id) { $rating->comment_ID = $id; add_comment_meta($id, '_wpkb_rating', $rating->rating, true); return true; } return false; }
function add_mention_users_to_notify_list($meta_id, $object_id, $meta_key, $meta_value) { if ($meta_key != 'users_mention_cp') { return; } $notify_user = get_comment_meta($object_id, 'notify_user'); if (!in_array($meta_value, $notify_user)) { add_comment_meta($object_id, 'notify_user', $meta_value); add_comment_meta($object_id, 'email_notify', 0, true); } return; }
function kt_save_comment_meta_data($comment_id) { $comment_obj = get_comment($comment_id); $post_id = get_post($comment_obj->comment_post_ID); $post_obj = get_post($post_id); if ($post_obj->post_type == 'shop' || $post_obj->post_type == 'tour') { if (isset($_POST['rating']) && $_POST['rating'] != '') { $rating = wp_filter_nohtml_kses($_POST['rating']); add_comment_meta($comment_id, '_kt_rating', $rating, TRUE); } } }
/** * Hooks the WP salesforce_comment_post action to save the first and last name data, * and send a Salesforce Web-to-Lead API request when an approved comment is added. * * @param int $id The ID of the comment * @param int|string $comment_approved Either 1, 0 or 'spam' as returned from wp_allow_comment * @return void **/ function salesforce_comment_post($comment_id, $comment_approved) { $options = get_option('salesforce2'); if (!$options['commentstoleads']) { return; } add_comment_meta($comment_id, 'author_first_name', $_POST['author_first_name']); add_comment_meta($comment_id, 'author_last_name', $_POST['author_last_name']); if (1 !== $comment_approved) { return; } salesforce_process_comment($comment_id); }
function delibera_discordar_comment_meta($comment_id) { $ndiscordaram = get_comment_meta($comment_id, "delibera_numero_discordar", true); if ($ndiscordaram == false || $ndiscordaram == "") { $ndiscordaram = array(); add_comment_meta($comment_id, 'delibera_numero_discordar', $ndiscordaram, true); } $discordaram = get_comment_meta($comment_id, "delibera_discordaram", true); if ($discordaram == false || $discordaram == "") { $discordaram = array(); add_comment_meta($comment_id, 'delibera_discordaram', $discordaram, true); } }
function delibera_curtir_comment_meta($comment_id) { $ncurtiram = get_comment_meta($comment_id, "delibera_numero_curtir", true); if ($ncurtiram == false || $ncurtiram == "") { $ncurtiram = array(); add_comment_meta($comment_id, 'delibera_numero_curtir', $ncurtiram, true); } $curtiram = get_comment_meta($comment_id, "delibera_curtiram", true); if ($curtiram == false || $curtiram == "") { $curtiram = array(); add_comment_meta($comment_id, 'delibera_curtiram', $curtiram, true); } }
function update_comment($comment_id) { if (!$this->should_process_terms()) { return; } $comment = get_comment($comment_id); delete_comment_meta($comment_id, $this->meta_key); $terms = $this->update_comment_terms($comment_id, $comment); foreach ($terms as $term) { add_comment_meta($comment_id, $this->meta_key, $term); } $this->update_terms($comment->comment_post_ID); }
function vortex_system_activate_plugin() { $all_comments = get_comments(); foreach ($all_comments as $comment) { add_comment_meta($comment->comment_ID, 'vortex_system_likes', 0, true); add_comment_meta($comment->comment_ID, 'vortex_system_dislikes', 0, true); } $all_posts = get_posts(array('posts_per_page' => -1)); foreach ($all_posts as $post) { add_post_meta($post->ID, 'vortex_system_likes', 0, true); add_post_meta($post->ID, 'vortex_system_dislikes', 0, true); } }
/** * add a notification for a author of media on new comment on media * @global type $bp * @param array $args contains comment descriptions */ function add_comment_notify($args) { $post_author_id = get_post($args['comment_post_ID'])->post_author; $post_id = $args['comment_post_ID']; $comment_id = $args['comment_id']; $user_id = $args['user_id']; if ($post_author_id == $user_id) { return; } $comment_notification_id = $this->add_notification($post_id, $post_author_id, $args['user_id']); if (false != $comment_notification_id) { add_comment_meta($comment_id, 'comment_notification_id', $comment_notification_id); } }
function save_comment_meta_data($comment_id) { if (get_post_type($_POST['comment_post_ID']) != 'course') { return; } if (isset($_POST['review_title']) && $_POST['review_title'] != '') { $title = wp_filter_nohtml_kses($_POST['review_title']); add_comment_meta($comment_id, 'review_title', $title); } if (isset($_POST['review_rating']) && $_POST['review_rating'] != '') { $rating = wp_filter_nohtml_kses($_POST['review_rating']); add_comment_meta($comment_id, 'review_rating', $rating); } }
/** * Insert a new comment * * @param array $commentdata * @param array $files * @return int */ function create($commentdata, $files = array()) { $user = wp_get_current_user(); $commentdata['comment_author_IP'] = preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']); $commentdata['comment_agent'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 254); $commentdata['comment_author'] = $user->display_name; $commentdata['comment_author_email'] = $user->user_email; $comment_id = wp_insert_comment($commentdata); if ($comment_id) { add_comment_meta($comment_id, '_files', $files); $this->associate_file($files, $commentdata['comment_post_ID'], $_POST['project_id']); } do_action('cpm_comment_new', $comment_id, $_POST['project_id'], $commentdata); return $comment_id; }
/** * Add new comment to WP post * * @param int $postId * @param array $comment * @return bool|int created comment id or false on failure */ public static function addPostComment($postId, $comment) { $filter = array('meta_key' => 'external_id', 'meta_value' => $comment['id']); \Plugin\Service::pingDB(); if (get_comments($filter)) { $result = false; } else { $wpComment = array('comment_post_ID' => $postId, 'comment_author' => $comment['from']['name'], 'comment_author_url' => 'http://facebook.com/' . $comment['from']['id'], 'comment_date' => date('Y-m-d H:i:s', strtotime($comment['created_time'])), 'comment_content' => wp_strip_all_tags($comment['message']), 'comment_approved' => 1); $commentId = wp_insert_comment($wpComment); if ($commentId) { add_comment_meta($commentId, 'external_id', $comment['id']); } $result = $commentId; } return $result; }
public function test_sends_highlander_comment_meta_with_comment() { $wpcom_user_id = 101; $sig = 'abcd1234'; $comment_ID = $this->comment->comment_ID; add_comment_meta($comment_ID, 'hc_post_as', 'wordpress', true); add_comment_meta($comment_ID, 'hc_wpcom_id_sig', $sig, true); add_comment_meta($comment_ID, 'hc_foreign_user_id', $wpcom_user_id, true); // re-save the comment wp_set_comment_status($comment_ID, 'hold'); $this->client->do_sync(); $this->client->do_sync(); $event = $this->server_event_storage->get_most_recent_event(); $synced_comment = $event->args[1]; $this->assertObjectHasAttribute('meta', $synced_comment); $this->assertEquals('wordpress', $synced_comment->meta['hc_post_as']); $this->assertEquals('abcd1234', $synced_comment->meta['hc_wpcom_id_sig']); $this->assertEquals(101, $synced_comment->meta['hc_foreign_user_id']); }
function add_notice_for_added_members($meta_id, $object_id, $meta_key, $meta_value) { if ($meta_key != 'members-cp-posts-sql') { return; } $comment_check = get_comments(array('post_id' => $object_id, 'post_type' => 'cases', 'type' => 'event_case_member_added', 'meta_key' => 'member_id', 'meta_value' => $meta_value)); //if (! empty($comment_check)) return; $user_ID = get_current_user_id(); $comment_id = wp_insert_comment(array('comment_post_ID' => $object_id, 'comment_author' => 'CasePress', 'comment_content' => 'Добавлен участник - ' . get_the_title($meta_value), 'comment_type' => 'event_case_member_added', 'user_ID' => $user_ID, 'comment_approved' => true)); //exit(var_dump($comment_id)); //Добавляем ИД участника в мету add_comment_meta($comment_id, 'member_id', $meta_value, true); //Если у персоны есть пользователь, то добавить в план уведомлений $user_id_member = get_user_by_person($meta_value); if ($user_id_member > 0) { add_comment_meta($comment_id, 'email_notify', 0, true); add_comment_meta($comment_id, 'notify_user', $user_id_member, true); } return; }
function email_notifications_for_users() { //error_log('********************** Запланированный хук ***************************'); $comments = get_comments(array('status' => 'approve', 'meta_key' => 'email_notify', 'meta_value' => '0', 'number' => 10)); foreach ($comments as $comment) { //error_log($comment->comment_author . '<br />' . $comment->comment_content); $comment_id = $comment->comment_ID; $users = get_comment_meta($comment_id, 'notify_user'); $users_notified = get_comment_meta($comment_id, 'notified_user'); foreach ($users as $nuser_id) { //Если участник забанен, то не нужно добавлять в список уведомлений $userdata = new WP_User($nuser_id); $user_role = array_shift($userdata->roles); if ($user_role == 'banned') { continue; } //тут не плохо было бы проверить отправлено данному пользователю уже уведомление или нет if (in_array($nuser_id, $users_notified)) { continue; } //если автор комментария ест в участниках, то ему уведомление на почту не отправлять, но отмечать как уведомленный if ($comment->user_id == $nuser_id) { add_comment_meta($comment_id, 'notified_user', $nuser_id); continue; } //тут функция отправки, которая возвращает результат отправки if ($this->send_email($nuser_id, $comment)) { // если все хорошо то записываем пользователя в список отправленных уведомлений add_comment_meta($comment_id, 'notified_user', $nuser_id); } } $users_notified = get_comment_meta($comment_id, 'notified_user'); //if both lists equal - add tag about all ok if ($users == $users_notified) { update_comment_meta($comment_id, 'email_notify', '1', '0'); } } }
function add_users_for_notice_if_reply_cp($comment_id, $comment) { //Если у коммента нет родителя, то возврат if (empty($comment->comment_parent)) { return; } //Если у родительского коммента нет пользователя, то возврат $comment_parent = get_comment($comment->comment_parent); if (empty($comment_parent->user_id)) { return; } /* //get post ID and $post $post_id = $comment->comment_post_ID; $r = get_post( $post_id ); //it is cases? if ( $r->post_type != 'cases') return; */ //add tag for plan email add_comment_meta($comment_id, 'email_notify', 0, true); add_comment_meta($comment_id, 'notify_user', $comment_parent->user_id); }
/** * Update the event journal. * * @since 1.3 * @param arr $data * @param arr $meta * @return: int|bool comment_id or false on failure */ function mdjm_add_journal($args = array(), $meta = array()) { // Return if journaling is disabled. if (!mdjm_get_option('journaling', false)) { return false; } $defaults = array('user_id' => get_current_user_id(), 'event_id' => '', 'comment_content' => '', 'comment_type' => 'mdjm-journal'); $data = wp_parse_args($args, $defaults); // Make sure we have the required data if (empty($data['comment_content']) || empty($data['event_id'])) { return false; } $comment_author = !empty($data['user_id']) ? get_userdata($data['user_id']) : 'mdjm'; $comment_data = apply_filters('mdjm_add_journal', array('comment_post_ID' => (int) $data['event_id'], 'comment_author' => $comment_author != 'mdjm' ? $comment_author->display_name : 'MDJM', 'comment_author_email' => $comment_author != 'mdjm' ? $comment_author->user_email : mdjm_get_option('system_email'), 'comment_author_IP' => '', 'comment_agent' => '', 'comment_author_url' => '', 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1), 'comment_content' => $data['comment_content'], 'comment_type' => 'mdjm-journal', 'user_id' => $comment_author != 'mdjm' ? $comment_author->ID : '0', 'comment_parent' => 0, 'comment_approved' => 1)); // Filter the comment data before inserting $comment_data = apply_filters('preprocess_comment', $comment_data); $comment_data = wp_filter_comment($comment_data); // Disable comment duplication check filter remove_filter('commentdata', 'comment_duplicate_trigger'); do_action('mdjm_pre_add_journal', $data, $meta, $comment_data); // Insert the comment $comment_id = wp_insert_comment($comment_data); if (!$comment_id) { return false; } $comment_meta = array('mdjm_type' => !empty($meta['type']) ? $meta['type'] : 'mdjm-journal', 'mdjm_visibility' => !empty($meta['visibility']) ? $meta['visibility'] : '0', 'mdjm_notify' => !empty($meta['notify']) ? $meta['notify'] : '', 'mdjm_to' => !empty($meta['to']) ? $meta['to'] : '', 'mdjm_isread' => !empty($meta['isread']) ? $meta['isread'] : ''); $comment_meta = wp_parse_args($meta, $comment_meta); foreach ($comment_meta as $key => $value) { if (!empty($value)) { add_comment_meta($comment_id, $key, $value, false); } } // Enable comment duplication check filter add_filter('commentdata', 'comment_duplicate_trigger'); do_action('mdjm_post_add_journal', $data, $meta, $comment_data); return $comment_id; }
/** * Adds a note (comment) to the order * * @param string $note Note to add * @param int $is_customer_note (default: 0) Is this a note for the customer? * @param bool added_by_user Was the note added by a user? * @return int Comment ID */ public function add_order_note($note, $is_customer_note = 0, $added_by_user = false) { if (is_user_logged_in() && current_user_can('edit_shop_order', $this->id) && $added_by_user) { $user = get_user_by('id', get_current_user_id()); $comment_author = $user->display_name; $comment_author_email = $user->user_email; } else { $comment_author = __('WooCommerce', 'woocommerce'); $comment_author_email = strtolower(__('WooCommerce', 'woocommerce')) . '@'; $comment_author_email .= isset($_SERVER['HTTP_HOST']) ? str_replace('www.', '', $_SERVER['HTTP_HOST']) : 'noreply.com'; $comment_author_email = sanitize_email($comment_author_email); } $comment_post_ID = $this->id; $comment_author_url = ''; $comment_content = $note; $comment_agent = 'WooCommerce'; $comment_type = 'order_note'; $comment_parent = 0; $comment_approved = 1; $commentdata = apply_filters('woocommerce_new_order_note_data', compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_agent', 'comment_type', 'comment_parent', 'comment_approved'), array('order_id' => $this->id, 'is_customer_note' => $is_customer_note)); $comment_id = wp_insert_comment($commentdata); if ($is_customer_note) { add_comment_meta($comment_id, 'is_customer_note', 1); do_action('woocommerce_new_customer_note', array('order_id' => $this->id, 'customer_note' => $commentdata['comment_content'])); } return $comment_id; }
/** * Create new posts based on import information * * Posts marked as having a parent which doesn't exist will become top level items. * Doesn't create a new post if: the post type doesn't exist, the given post ID * is already noted as imported or a post with the same title and date already exists. * Note that new/updated terms, comments and meta are imported for the last of the above. */ function process_posts() { foreach ($this->posts as $post) { if (!post_type_exists($post['post_type'])) { printf(__('Failed to import “%s”: Invalid post type %s', 'radium'), esc_html($post['post_title']), esc_html($post['post_type'])); echo '<br />'; continue; } if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) { continue; } if ($post['status'] == 'auto-draft') { continue; } if ('nav_menu_item' == $post['post_type']) { $this->process_menu_item($post); continue; } $post_type_object = get_post_type_object($post['post_type']); $post_exists = post_exists($post['post_title'], '', $post['post_date']); if ($post_exists && get_post_type($post_exists) == $post['post_type']) { printf(__('%s “%s” already exists.', 'radium'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); echo '<br />'; $comment_post_ID = $post_id = $post_exists; } else { $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($this->processed_posts[$post_parent])) { $post_parent = $this->processed_posts[$post_parent]; // otherwise record the parent for later } else { $this->post_orphans[intval($post['post_id'])] = $post_parent; $post_parent = 0; } } // map the post author $author = sanitize_user($post['post_author'], true); if (isset($this->author_mapping[$author])) { $author = $this->author_mapping[$author]; } else { $author = (int) get_current_user_id(); } $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); if ('attachment' == $postdata['post_type']) { $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; // try to use _wp_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata['upload_date'] = $post['post_date']; if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { if ($meta['key'] == '_wp_attached_file') { if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { $postdata['upload_date'] = $matches[0]; } break; } } } $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url); } else { $comment_post_ID = $post_id = wp_insert_post($postdata, true); } if (is_wp_error($post_id)) { printf(__('Failed to import %s “%s”', 'radium'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $post_id->get_error_message(); } echo '<br />'; continue; } if ($post['is_sticky'] == 1) { stick_post($post_id); } } // map pre-import ID to local ID $this->processed_posts[intval($post['post_id'])] = (int) $post_id; // add categories, tags and other terms if (!empty($post['terms'])) { $terms_to_set = array(); foreach ($post['terms'] as $term) { // back compat with WXR 1.0 map 'tag' to 'post_tag' $taxonomy = 'tag' == $term['domain'] ? 'post_tag' : $term['domain']; $term_exists = term_exists($term['slug'], $taxonomy); $term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists; if (!$term_id) { $t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug'])); if (!is_wp_error($t)) { $term_id = $t['term_id']; } else { printf(__('Failed to import %s %s', 'radium'), esc_html($taxonomy), esc_html($term['name'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $t->get_error_message(); } echo '<br />'; continue; } } $terms_to_set[$taxonomy][] = intval($term_id); } foreach ($terms_to_set as $tax => $ids) { $tt_ids = wp_set_post_terms($post_id, $ids, $tax); } unset($post['terms'], $terms_to_set); } // add/update comments if (!empty($post['comments'])) { $num_comments = 0; $inserted_comments = array(); foreach ($post['comments'] as $comment) { $comment_id = $comment['comment_id']; $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; $newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); if (isset($this->processed_authors[$comment['comment_user_id']])) { $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; } } ksort($newcomments); foreach ($newcomments as $key => $comment) { // if this is a new post we can skip the comment_exists() check if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) { if (isset($inserted_comments[$comment['comment_parent']])) { $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; } $comment = wp_filter_comment($comment); $inserted_comments[$key] = wp_insert_comment($comment); foreach ($comment['commentmeta'] as $meta) { $value = maybe_unserialize($meta['value']); add_comment_meta($inserted_comments[$key], $meta['key'], $value); } $num_comments++; } } unset($newcomments, $inserted_comments, $post['comments']); } // add/update post meta if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = apply_filters('import_post_meta_key', $meta['key']); $value = false; if ('_edit_last' == $key) { if (isset($this->processed_authors[intval($meta['value'])])) { $value = $this->processed_authors[intval($meta['value'])]; } else { $key = false; } } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } add_post_meta($post_id, $key, $value); do_action('import_post_meta', $post_id, $key, $value); // if the post has a featured image, take note of this in case of remap if ('_thumbnail_id' == $key) { $this->featured_images[$post_id] = (int) $value; } } } } } unset($this->posts); }