public function ap_delete_post() { $args = explode('-', sanitize_text_field($_REQUEST['args'])); $action = 'delete_post_' . $args[0]; if (!ap_user_can_delete($args[0])) { $result = array('action' => false, 'message' => __('No Permission', 'ap')); } elseif (wp_verify_nonce($args[1], $action)) { $post = get_post($args[0]); wp_trash_post($args[0]); if ($post->post_type == 'question') { $result = array('action' => 'question', 'redirect_to' => get_permalink(ap_opt('base_page')), 'message' => __('Question deleted successfully.', 'ap')); } else { $current_ans = ap_count_ans($post->post_parent); $count_label = sprintf(_n('1 Answer', '%d Answers', $current_ans, 'ap'), $current_ans); $remove = !$current_ans ? true : false; $result = array('action' => 'answer', 'div' => '#answer_' . $args[0], 'count' => $current_ans, 'count_label' => $count_label, 'remove' => $remove, 'message' => __('Answer deleted successfully.', 'ap')); } } die(json_encode($result)); }
/** * Post actions buttons * @return string * @param array $disable * @return void * @since 2.0 */ function ap_post_actions_buttons($disable = array()) { global $post; if (!$post->post_type == 'question' || !$post->post_type == 'answer') { return; } $actions = array(); /** * Select answer button * @var string */ if ($post->post_type == 'answer') { $actions['select_answer'] = ap_select_answer_btn_html($post->ID); } /** * Comment button */ if (ap_user_can_comment()) { $actions['comment'] = ap_comment_btn_html(); } $actions['status'] = ap_post_change_status_btn_html($post->ID); /** * edit question link */ if (ap_user_can_edit_question($post->ID) && $post->post_type == 'question') { $actions['dropdown']['edit_question'] = ap_edit_post_link_html(); } if (ap_user_can_edit_ans($post->ID) && $post->post_type == 'answer') { $actions['dropdown']['edit_answer'] = ap_edit_post_link_html(); } if (is_user_logged_in()) { $actions['dropdown']['flag'] = ap_flag_btn_html(); } if (is_super_admin() && $post->post_type == 'question') { $actions['dropdown']['featured'] = ap_featured_post_btn(); } if (ap_user_can_delete($post->ID) && $post->post_status != 'trash') { $actions['dropdown']['delete'] = ap_post_delete_btn_html(); } if (ap_user_can_delete($post->ID)) { $actions['dropdown']['permanent_delete'] = ap_post_permanent_delete_btn_html(); } /** * FILTER: ap_post_actions_buttons * For filtering post actions buttons * @var string * @since 2.0 */ $actions = apply_filters('ap_post_actions_buttons', $actions); if (!empty($actions) && count($actions) > 0) { echo '<ul id="ap_post_actions_' . $post->ID . '" class="ap-q-actions ap-ul-inline clearfix">'; foreach ($actions as $k => $action) { if (!empty($action) && $k != 'dropdown' && !in_array($k, $disable)) { echo '<li class="ap-post-action ap-action-' . $k . '">' . $action . '</li>'; } } if (!empty($actions['dropdown'])) { echo '<li class="ap-post-action dropdown">'; echo '<div id="ap_post_action_' . $post->ID . '" class="ap-dropdown">'; echo '<a class="apicon-ellipsis more-actions ap-tip ap-dropdown-toggle" title="' . __('More action', 'ap') . '" href="#"></a>'; echo '<ul class="ap-dropdown-menu">'; foreach ($actions['dropdown'] as $sk => $sub) { echo '<li class="ap-post-action ap-action-' . $sk . '">' . $sub . '</li>'; } echo '</ul>'; echo '</div>'; echo '</li>'; } echo '</ul>'; } }
/** * Output frontend post delete button. * * @param int $post_id * @param bool $echo * * @return void|string */ function ap_post_delete_btn_html($post_id = false, $echo = false) { if ($post_id === false) { $post_id = get_the_ID(); } if (ap_user_can_delete($post_id)) { $action = 'delete_post_' . $post_id; $nonce = wp_create_nonce($action); $output = '<a href="#" class="delete-btn" data-action="ap_delete_post" data-query="post_id=' . $post_id . '&__nonce=' . $nonce . '&ap_ajax_action=delete_post" title="' . __('Delete', 'ap') . '">' . __('Delete', 'ap') . '</a>'; if ($echo) { echo $output; } else { return $output; } } }
/** * Process ajax trash posts callback */ public function delete_post() { $post_id = (int) $_POST['post_id']; $action = 'delete_post_' . $post_id; if (!wp_verify_nonce($_POST['__nonce'], $action) || !ap_user_can_delete($post_id)) { $this->something_wrong(); } $post = get_post($post_id); if (time() > get_the_time('U', $post->ID) + (int) ap_opt('disable_delete_after') && !is_super_admin()) { $this->send(array('message_type' => 'warning', 'message' => sprintf(__('This post was created %s, its locked hence you cannot delete it.', 'anspress-question-answer'), ap_human_time(get_the_time('U', $post->ID))))); } wp_trash_post($post_id); if ($post->post_type == 'question') { do_action('ap_wp_trash_question', $post_id); $this->send(array('action' => 'delete_question', 'do' => array('redirect' => ap_base_page_link()), 'message' => 'question_moved_to_trash')); } else { do_action('ap_wp_trash_answer', $post_id); $current_ans = ap_count_published_answers($post->post_parent); $count_label = sprintf(_n('1 Answer', '%d Answers', $current_ans, 'anspress-question-answer'), $current_ans); $remove = !$current_ans ? true : false; $this->send(array('action' => 'delete_answer', 'div_id' => '#answer_' . $post_id, 'count' => $current_ans, 'count_label' => $count_label, 'remove' => $remove, 'message' => 'answer_moved_to_trash', 'view' => array('answer_count' => $current_ans, 'answer_count_label' => $count_label))); } }
function ap_post_delete_btn_html($post_id = false) { if (!$post_id) { $post_id = get_the_ID(); } if (ap_user_can_delete($post_id)) { $action = 'delete_post_' . $post_id; $nonce = wp_create_nonce($action); echo '<a href="#" class="delete-btn" data-button="ap-delete-post" data-args="' . $post_id . '-' . $nonce . '" title="' . __('Delete', 'ap') . '">' . __('Delete', 'ap') . '</a>'; } }