public function at_ajax_add_tournament_round() { check_ajax_referer(ajax_nonce_string, 'security'); if (!is_admin()) { exit('Get oouuuttaaa heeeere.'); } global $at_functions, $at_tournament; $at_functions = new at_functions(); $at_tournament = new at_tournament(); // get tournament id for relevant tournament $tournament_id = $_REQUEST['postId']; if (!is_numeric($tournament_id) || $tournament_id <= 0) { exit('Get oouuuttaaa heeeere.'); } // get post information to determine tournament_slug $tournament = get_post($tournament_id); //$tournament_categories = wp_get_object_terms( $tournament_id, 'category' ); // unused so far // get post slug to determine original post and newest post $tournament_slug = get_post_meta($tournament_id, at_tournament_slug, true); // get oldest post with tournament_slug $result = get_posts(array('post_type' => at_post_type_tournament, 'posts_per_page' => 1, 'meta_query' => array(array('key' => at_tournament_slug, 'value' => $tournament_slug, 'compare' => 'AND')), 'orderby' => 'date', 'order' => 'ASC')); if (!empty($result)) { $original_tournament = $result[0]; } else { return false; } // get newest post with tournament_slug $result = get_posts(array('post_type' => at_post_type_tournament, 'posts_per_page' => 1, 'meta_query' => array(array('key' => at_tournament_slug, 'value' => $tournament_slug, 'compare' => 'AND')), 'orderby' => 'date', 'order' => 'DESC')); if (!empty($result)) { $newest_tournament = $result[0]; } else { return false; } // get newest tournament round number $newest_tournament_round = get_post_meta($newest_tournament->ID, at_tournament_round, true); // increment round number for new post $new_tournament_round = $newest_tournament_round + 1; // get current (admin) user id for new post insertion $current_user_id = get_current_user_id(); // get full category list to update categories for new post $at_categories = $at_functions->at_get_categories(); // get category for current tournament $tournament_category = get_category_by_slug($tournament_slug); // build new tournament post array $new_round_post = array('post_type' => at_post_type_tournament, 'comment_status' => 'closed', 'post_title' => $original_tournament->post_title . ' ' . $new_tournament_round, 'post_author' => $current_user_id, 'post_parent' => 0, 'post_status' => 'draft'); // insert new tournament post and return post ID on success $new_round_id = $at_functions->at_insert_post($new_round_post); // if insertion is successful, update tags & categories if (!empty($new_round_id)) { add_post_meta($new_round_id, at_tournament_slug, $tournament_slug); add_post_meta($new_round_id, at_tournament_round, $new_tournament_round); $result = wp_set_object_terms($new_round_id, array($at_categories[at_post_type_tournament], $tournament_category->term_id), 'category'); // build round id & link for return to view $new_round_link = get_edit_post_link($new_round_id, '&'); wp_send_json(array('round_id' => $new_round_id, 'round_link' => $new_round_link)); } exit; }
public function at_new_tournament($post) { global $at_functions; if (!isset($at_functions)) { $at_functions = new at_functions(); } // empty datetime $empty_datetime = "0000-00-00 00:00:00"; $at_categories = $at_functions->at_get_categories(); $post_meta = get_post_meta($post->ID); // if new tournament, start round number at 1 if (empty($post_meta[at_tournament_round])) { add_post_meta($post->ID, at_tournament_round, 1); } // if new tournament, get new tournament slug $tournament_slug = $this->at_make_tournament_slug($post->post_title); if (empty($post_meta[at_tournament_slug])) { add_post_meta($post->ID, at_tournament_slug, $tournament_slug); } else { $tournament_slug = $post_meta[at_tournament_slug][0]; } $category = get_category_by_slug($tournament_slug); if (empty($category)) { $tournament_category = wp_insert_category(array('cat_name' => $post->post_title, 'category_nicename' => $tournament_slug)); } else { $tournament_category = $category->term_id; } $post_categories = wp_get_post_categories($post->ID); if (empty($post_categories)) { wp_set_object_terms($post->ID, array($at_categories[at_post_type_tournament], $tournament_category), 'category'); } $args = array('post_parent' => $post->ID, 'posts_per_page' => 5); $post_children = get_children($args); if (empty($post_children)) { $rules_post = array('post_type' => $post->post_type, 'comment_status' => 'closed', 'post_title' => 'Rules', 'post_author' => $post->post_author, 'post_parent' => $post->ID, 'post_status' => 'draft'); $samplepack_post = array('post_type' => $post->post_type, 'comment_status' => 'closed', 'post_title' => 'Sample Pack', 'post_author' => $post->post_author, 'post_parent' => $post->ID, 'post_status' => 'draft'); $submit_post = array('post_type' => $post->post_type, 'comment_status' => 'closed', 'post_title' => 'Submit', 'post_author' => $post->post_author, 'post_content' => '[' . at_insert_submit_entry . ']', 'post_parent' => $post->ID, 'post_status' => 'draft'); $voting_post = array('post_type' => $post->post_type, 'comment_status' => 'closed', 'post_title' => 'Voting', 'post_author' => $post->post_author, 'post_content' => '[' . at_insert_voting_list . ']', 'post_parent' => $post->ID, 'post_status' => 'draft'); $results_post = array('post_type' => $post->post_type, 'comment_status' => 'closed', 'post_title' => 'Results', 'post_author' => $post->post_author, 'post_content' => '[' . at_insert_results_list . ']', 'post_parent' => $post->ID, 'post_status' => 'draft'); // Insert Tournament Rules child post $result = $at_functions->at_insert_post($rules_post); if ($result) { // add rules post type meta data to post add_post_meta($result, at_post_type, at_post_type_rules_page); // add empty end time meta data to post add_post_meta($result, at_post_expires, $empty_datetime); // add category slug to post wp_set_object_terms($result, array($at_categories[at_post_type_rules_page]), 'category'); } // Insert Tournament Sample Pack child post $result = $at_functions->at_insert_post($samplepack_post); if ($result) { // add samplepack post type to post add_post_meta($result, at_post_type, at_post_type_sample_pack_page); // add empty end time meta data to post add_post_meta($result, at_post_expires, $empty_datetime); // add category slug to post wp_set_object_terms($result, array($at_categories[at_post_type_sample_pack_page]), 'category'); } // Insert Tournament Submit Entry child post $result = $at_functions->at_insert_post($submit_post); if ($result) { // add submit post type to post add_post_meta($result, at_post_type, at_post_type_submit_page); // add empty end time meta data to post add_post_meta($result, at_post_expires, $empty_datetime); // add category slug to post wp_set_object_terms($result, array($at_categories[at_post_type_submit_page]), 'category'); } // Insert Tournament Voting child post $result = $at_functions->at_insert_post($voting_post); if ($result) { // add voting post type to post add_post_meta($result, at_post_type, at_post_type_voting_page); // add empty end time meta data to post add_post_meta($result, at_post_expires, $empty_datetime); // add category slug to post wp_set_object_terms($result, array($at_categories[at_post_type_voting_page]), 'category'); } // Insert Tournament Results child post $result = $at_functions->at_insert_post($results_post); if ($result) { // add results post type to post add_post_meta($result, at_post_type, at_post_type_results_page); // add empty end time meta data to post add_post_meta($result, at_post_expires, $empty_datetime); // add category slug to post wp_set_object_terms($result, array($at_categories[at_post_type_results_page]), 'category'); } } }