ap_post_actions( mixed $_post = null )

Description #

Return post actions array.

Parameters #

  • $_post
    mixed (Optional) Post. Default value: null

Changelog #

VersionDescription
3.0.0Introduced.

Source #

File: includes/theme.php

function ap_post_actions( $_post = null ) {
	$_post = ap_get_post( $_post );

	$actions = array();

	if ( ! in_array( $_post->post_type, array( 'question', 'answer' ), true ) ) {
		return $actions;
	}

	// Featured link.
	if ( 'question' === $_post->post_type ) {
		$actions[] = ap_featured_post_args( $_post->ID );
	}

	// Question close action.
	if ( ap_user_can_close_question() && 'question' === $_post->post_type ) {
		$nonce       = wp_create_nonce( 'close_' . $_post->ID );
		$close_label = $_post->closed ? __( 'Open', 'anspress-question-answer' ) : __( 'Close', 'anspress-question-answer' );
		$close_title = $_post->closed ? __( 'Open this question for new answers', 'anspress-question-answer' ) : __( 'Close this question for new answer.', 'anspress-question-answer' );

		$actions[] = array(
			'cb'    => 'close',
			'icon'  => 'apicon-check',
			'query' => array(
				'nonce'   => $nonce,
				'post_id' => $_post->ID,
			),
			'label' => $close_label,
			'title' => $close_title,
		);
	}

	// Edit link.
	if ( ap_user_can_edit_post( $_post->ID ) ) {
		$actions[] = array(
			'cb'    => 'edit',
			'label' => __( 'Edit', 'anspress-question-answer' ),
			'href'  => ap_post_edit_link( $_post ),
		);
	}

	// Flag link.
	$actions[] = ap_flag_btn_args( $_post );

	$status_args = ap_post_status_btn_args( $_post );

	if ( ! empty( $status_args ) ) {
		$actions[] = array(
			'label'  => __( 'Status', 'anspress-question-answer' ),
			'header' => true,
		);

		$actions   = array_merge( $actions, $status_args );
		$actions[] = array( 'header' => true );
	}

	$answers = ap_count_published_answers( $_post->ID );

	if ( ap_user_can_delete_post( $_post->ID ) && ( ! ap_opt( 'trashing_question_with_answer' ) || empty( $answers ) ) ) {
		if ( 'trash' === $_post->post_status ) {
			$label = __( 'Undelete', 'anspress-question-answer' );
			$title = sprintf(
				/* Translators: %s Question or Answer post type label for restoring a question or answer. */
				__( 'Restore this %s', 'anspress-question-answer' ),
				( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
			);
		} else {
			$label = __( 'Delete', 'anspress-question-answer' );
			$title = sprintf(
				/* Translators: %s Question or Answer post type label for deleting a question or answer. */
				__( 'Delete this %s (can be restored again)', 'anspress-question-answer' ),
				( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
			);
		}

		$actions[] = array(
			'cb'    => 'toggle_delete_post',
			'query' => array(
				'post_id' => $_post->ID,
				'__nonce' => wp_create_nonce( 'trash_post_' . $_post->ID ),
			),
			'label' => $label,
			'title' => $title,
		);
	}

	// Permanent delete link.
	if ( ap_user_can_permanent_delete( $_post->ID ) && ( ! ap_opt( 'deleting_question_with_answer' ) || empty( $answers ) ) ) {
		$actions[] = array(
			'cb'    => 'delete_permanently',
			'query' => array(
				'post_id' => $_post->ID,
				'__nonce' => wp_create_nonce( 'delete_post_' . $_post->ID ),
			),
			'label' => __( 'Delete Permanently', 'anspress-question-answer' ),
			'title' => sprintf(
				/* Translators: %s Question or Answer post type label for permanently deleting a question or answer. */
				__( 'Delete %s permanently (cannot be restored again)', 'anspress-question-answer' ),
				( 'question' === $_post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
			),
		);
	}

	// Convert question to a post.
	if ( ( is_super_admin() || current_user_can( 'manage_options' ) ) && 'question' === $_post->post_type ) {
		$actions[] = array(
			'cb'    => 'convert_to_post',
			'query' => array(
				'post_id' => $_post->ID,
				'__nonce' => wp_create_nonce( 'convert-post-' . $_post->ID ),
			),
			'label' => __( 'Convert to post', 'anspress-question-answer' ),
			'title' => __( 'Convert this question to blog post', 'anspress-question-answer' ),
		);
	}

	/**
	 * For filtering post actions buttons
	 *
	 * @var     array
	 * @since   2.0
	 */
	$actions = apply_filters( 'ap_post_actions', array_filter( $actions ) );
	return array_values( $actions );
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment