* @license GPL-3.0+ * @link https://anspress.io * @copyright 2014 Rahul Aryan */ if ( ! function_exists( '_deprecated_function' ) ) { require_once ABSPATH . WPINC . '/functions.php'; } /** * Send ajax response if there is error in validation class. * * @param object $validate Validation class. * @since 3.0.0 * @deprecated 4.1.0 */ function ap_form_validation_error_response( $validate ) { _deprecated_function( __FUNCTION__, '4.1.0' ); // If error in form then return. if ( $validate->have_error() ) { ap_ajax_json( array( 'success' => false, 'form' => $_POST['ap_form_action'], 'snackbar' => [ 'message' => __( 'Check missing fields and then re-submit.', 'anspress-question-answer' ), ], 'errors' => $validate->get_errors(), ) ); } } /** * Upload form. * * @param boolean|integer $post_id Post ID. * @return string * * @deprecated 4.1.0 This function was replaced by new form class. */ function ap_post_upload_form( $post_id = false ) { _deprecated_function( __FUNCTION__, '4.1.0', 'AnsPress\Form\Upload' ); if ( ! ap_user_can_upload() ) { return; } if ( false === $post_id ) { $media = get_posts( [ 'post_type' => 'attachment', 'title' => '_ap_temp_media', 'post_author' => get_current_user_id(), ] ); } else { $media = get_attached_media( '', $post_id ); } $label = sprintf( __( 'Insert images and attach media by %1$sselecting them%2$s', 'anspress-question-answer' ), '', '' ); $html = '
' . esc_html( $matches[1] ) . ''; } /** * Callback used in @see `ap_sanitize_description_field()`. * * @param array $matches Matched strings. * @return string * @deprecated 4.1.0 */ function ap_sanitize_description_field_code_content( $matches ) { return '
' . esc_html( $matches[1] ) . '';
}
/**
* Get all answer form fields.
*
* @param integer|boolean $question_id Post ID.
* @param integer|boolean $answer_id Answer ID.
* @return array
*
* @since 3.0.0
* @deprecated 4.1.0
*/
function ap_get_answer_form_fields( $question_id = false, $answer_id = false ) {
_deprecated_function( __FUNCTION__, '4.1.0' );
global $editing_post;
$editing = false;
if ( $answer_id && ap_user_can_edit_answer( (int) $answer_id ) ) {
$editing = true;
$editing_post = ap_get_post( (int) $answer_id, 'OBJECT', 'edit' );
}
$is_private = false;
if ( $editing ) {
$is_private = $editing_post->post_status == 'private_post' ? true : false;
}
$fields = array(
array(
'name' => 'description',
'type' => is_question() ? 'textarea' : 'editor',
'value' => ( $editing ? $editing_post->post_content : wp_kses_post( ap_isset_post_value( 'description', '' ) ) ),
'placeholder' => __( 'Your answer..', 'anspress-question-answer' ),
'settings' => ap_tinymce_editor_settings( 'answer' ),
'sanitize' => array( 'sanitize_description' ),
'validate' => array(
'required' => true,
'length_check' => ap_opt( 'minimum_ans_length' ),
),
),
array(
'name' => 'form_question_id',
'type' => 'hidden',
'value' => ( $editing ? $editing_post->post_parent : $question_id ),
'order' => 20,
),
);
// Add name fields if anonymous is allowed.
if ( ! is_user_logged_in() && ap_opt( 'allow_anonymous' ) ) {
$fields[] = array(
'name' => 'anonymous_name',
'label' => __( 'Name', 'anspress-question-answer' ),
'type' => 'text',
'placeholder' => __( 'Enter your name to display', 'anspress-question-answer' ),
'value' => ap_isset_post_value( 'name', '' ),
'order' => 12,
'sanitize' => array( 'strip_tags', 'sanitize_text_field' ),
);
}
// Add private field checkbox if enabled.
if ( ap_opt( 'allow_private_posts' ) ) {
$fields[] = array(
'name' => 'is_private',
'type' => 'checkbox',
'desc' => __( 'Only visible to admin and moderator.', 'anspress-question-answer' ),
'value' => $is_private,
'order' => 12,
'show_desc_tip' => false,
'sanitize' => array( 'only_boolean' ),
);
}
$fields[] = array(
'name' => 'ap_upload',
'type' => 'custom',
'html' => ap_post_upload_form( $editing ? $editing_post->ID : false ),
'order' => 11,
);
$fields[] = array(
'name' => 'action',
'type' => 'hidden',
'value' => 'ap_ajax',
'order' => 20,
);
$fields[] = array(
'name' => 'ap_ajax_action',
'type' => 'hidden',
'value' => 'answer_form',
'order' => 20,
);
if ( $editing ) {
$fields[] = array(
'name' => 'edit_post_id',
'type' => 'hidden',
'value' => $editing_post->ID,
'order' => 20,
'sanitize' => array( 'only_int' ),
);
}
/** This filter is documented in includes/class-form-hooks.php */
$fields = apply_filters( 'ap_answer_form_fields', array( 'fields' => $fields ), $editing );
return $fields['fields'];
}
/**
* Generate edit question form, this is a wrapper of ap_answer_form().
*
* @param integer $question_id Id of question.
* @return void
* @since 2.0.1
* @deprecated 4.1.0
*/
function ap_edit_answer_form( $question_id ) {
_deprecated_function( __FUNCTION__, '4.1.0' );
ap_answer_form( $question_id, true );
}
/**
* Return or echo hovercard data attribute.
*
* @param integer $user_id User id.
* @param boolean $echo Echo or return? default is true.
* @return string
*
* @deprecated 4.1.0
*/
function ap_hover_card_attributes( $user_id, $echo = true ) {
if ( $user_id > 0 ) {
$attr = ' data-userid="' . $user_id . '"';
if ( true !== $echo ) {
return $attr;
}
echo $attr; // xss okay.
}
}
/**
* TinyMCE editor setting
*
* @return array
* @since 3.0.0
* @deprecated 4.1.0 This is no longer required as of upload field introduction.
*/
function ap_tinymce_editor_settings( $type = 'question' ) {
_deprecated_function( __FUNCTION__, '4.1.0', 'This is no longer required as of upload field introduction.' );
$setting = array(
'textarea_rows' => 8,
'tinymce' => ap_opt( $type . '_text_editor' ) ? false : true,
'quicktags' => ap_opt( $type . '_text_editor' ) ? true : false,
'media_buttons' => false,
);
if ( ap_opt( $type . '_text_editor' ) ) {
$settings['tinymce'] = array(
'content_css' => ap_get_theme_url( 'css/editor.css' ),
'wp_autoresize_on' => true,
'statusbar' => false,
);
}
return apply_filters( 'ap_tinymce_editor_settings', $setting, $type );
}
/**
* Read env file of AnsPress.
*
* @return string
* @deprecated 4.1.0
*/
function ap_read_env() {
_deprecated_function( __FUNCTION__, '4.1.0' );
$file = ANSPRESS_DIR . '/env';
$cache = wp_cache_get( 'ap_env', 'ap' );
if ( false !== $cache ) {
return $cache;
}
if ( file_exists( $file ) ) {
// Get the contents of env file.
$content = file_get_contents( $file ); // @codingStandardsIgnoreLine.
wp_cache_set( 'ap_env', $content, 'ap' );
return $content;
}
}
/**
* Check if anspress environment is development.
*
* @return boolean
* @deprecated 4.1.0
*/
function ap_env_dev() {
_deprecated_function( __FUNCTION__, '4.1.0' );
if ( 'development' === ap_read_env() ) {
return true;
}
return false;
}
/**
* Insert and update answer.
*
* @param array $question_id Question ID.
* @param array $args Answer arguments.
* @param bool $wp_error Return wp error.
* @return bool|object|int
*
* @deprecated 4.1.0
*/
function ap_save_answer( $question_id, $args, $wp_error = false ) {
_deprecated_function( __FUNCTION__, '4.1.0' );
$question = ap_get_post( $question_id );
if ( isset( $args['is_private'] ) && $args['is_private'] ) {
$args['post_status'] = 'private_post';
}
$args = wp_parse_args(
$args, array(
'post_title' => $question->post_title,
'post_author' => get_current_user_id(),
'post_status' => 'publish',
'post_name' => '',
'comment_status' => 'open',
)
);
$args['post_content'] = apply_filters( 'ap_form_contents_filter', $args['post_content'] );
$args['post_type'] = 'answer';
$args['post_parent'] = $question_id;
if ( isset( $args['ID'] ) ) {
/**
* Can be used to modify `$args` before updating answer
*
* @param array $args Answer arguments.
* @since 2.0.1
*/
$args = apply_filters( 'ap_pre_update_answer', $args );
} else {
/**
* Can be used to modify args before inserting answer.
*
* @param array $args Answer arguments.
* @since 2.0.1
*/
$args = apply_filters( 'ap_pre_insert_answer', $args );
}
$post_id = wp_insert_post( $args, true );
if ( true === $wp_error && is_wp_error( $post_id ) ) {
return $post_id;
}
if ( $post_id ) {
$qameta_args = [ 'last_updated' => current_time( 'mysql' ) ];
if ( isset( $args['anonymous_name'] ) ) {
$qameta_args['fields'] = [ 'anonymous_name' => $args['anonymous_name'] ];
}
ap_insert_qameta( $post_id, $qameta_args );
$activity_type = isset( $args['ID'] ) ? 'edit_answer' : 'new_answer';
// Add answer activity meta.
ap_update_post_activity_meta( $post_id, $activity_type, get_current_user_id() );
ap_update_post_activity_meta( $question->ID, $activity_type, get_current_user_id() );
if ( ap_isset_post_value( 'ap-medias' ) ) {
$ids = ap_sanitize_unslash( 'ap-medias', 'r' );
ap_set_media_post_parent( $ids, $post_id );
}
}
return $post_id;
}
/**
* Display fields group options. Uses AnsPress_Form to renders fields.
*
* @since 2.0.0
* @deprecated 4.1.0 Replaced by new form class.
*/
function ap_option_group_fields() {
_deprecated_function( __FUNCTION__, '4.1.0' );
$groups = ap_get_option_groups();
$active = ap_sanitize_unslash( 'option_page', 'request', 'general' );
if ( empty( $groups ) && is_array( $groups ) ) {
return;
}
$group = $groups[ $active ];
foreach ( (array) $group['sections'] as $section_slug => $section ) {
$fields = $section['fields'];
if ( is_array( $fields ) ) {
$fields[] = array(
'name' => 'action',
'type' => 'hidden',
'value' => 'anspress_options',
);
$fields[] = array(
'name' => 'fields_group',
'type' => 'hidden',
'value' => $active,
);
$fields[] = array(
'name' => 'ap_active_section',
'type' => 'hidden',
'value' => $section_slug,
);
$args = array(
'name' => 'options_form',
'is_ajaxified' => false,
'submit_button' => __( 'Save options', 'anspress-question-answer' ),
'nonce_name' => 'nonce_option_form',
'fields' => $fields,
'action' => admin_url( 'admin-post.php' ),
);
$form = new AnsPress_Form( $args );
echo '