settings->get_setting( 'woocommerce-faqs-location' ) ) or $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs-location' ) == 'tabs' ) { add_filter( 'woocommerce_product_tabs', array( $this, 'add_woocommerce_tab' ) ); }
elseif ( $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs-location' ) == 'before_add_to_cart' ) { add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'faq_content' ) ); }
elseif ( $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs-location' ) == 'after_add_to_cart' ) { add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'faq_content' ) ); }
elseif ( $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs-location' ) == 'after_product_summary' ) { add_action( 'woocommerce_after_single_product_summary', array( $this, 'faq_content' ) ); }
elseif ( $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs-location' ) == 'after_product' ) { add_action( 'woocommerce_after_single_product', array( $this, 'faq_content' ) ); }
}
/**
* Adds an FAQ tab to WooCommerce
* @since 2.0.0
*/
public function add_woocommerce_tab( $tabs ) {
global $product;
global $ewd_ufaq_controller;
if ( ! $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs' ) ) { return $tabs; }
$product_post = ( $ewd_ufaq_controller->settings->get_setting( 'woocommerce-use-product' ) and is_object( $product ) ) ? get_post( $product->get_id() ) : get_post( get_the_id() );
$ufaq_product_category = get_term_by( 'name', $product_post->post_title, EWD_UFAQ_FAQ_CATEGORY_TAXONOMY );
$wc_cats = get_the_terms( $product_post, 'product_cat' );
$ufaq_wccat_category = false;
if ( $wc_cats ) {
foreach ( $wc_cats as $wc_cat ) {
if ( get_term_by( 'name', $wc_cat->name, EWD_UFAQ_FAQ_CATEGORY_TAXONOMY ) ) { $ufaq_wccat_category = true; }
}
}
$current_faqs = is_array( get_post_meta( $product_post->ID, 'EWD_UFAQ_WC_Selected_FAQs', true ) ) ? array_filter( get_post_meta( $product_post->ID, 'EWD_UFAQ_WC_Selected_FAQs', true ) ) : array();
if ( ( $ufaq_product_category or $ufaq_wccat_category or ! empty( $current_faqs ) ) ) {
$tabs['faq_tab'] = array(
'title' => $ewd_ufaq_controller->settings->get_setting( 'label-woocommerce-tab' ),
'priority' => 50,
'callback' => array( $this, 'faq_content' )
);
}
return $tabs;
}
public function admin_enqueue( $hook ) {
global $post;
global $ewd_ufaq_controller;
if ( ! $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs' ) ) { return; }
if ( $hook != 'edit.php' and $hook != 'post-new.php' and $hook != 'post.php' ) { return; }
if ( ! isset( $post->post_type ) or $post->post_type != 'product' ) { return; }
wp_enqueue_style( 'ewd-ufaq-wc-admin-css', EWD_UFAQ_PLUGIN_URL . '/assets/css/ewd-ufaq-wc-admin.css', array(), EWD_UFAQ_VERSION );
wp_enqueue_script( 'ewd-ufaq-wc-admin-js', EWD_UFAQ_PLUGIN_URL . '/assets/js/ewd-ufaq-wc-admin.js', array( 'jquery' ), EWD_UFAQ_VERSION );
wp_localize_script(
'ewd-ufaq-wc-admin-js',
'ewd_ufaq_wc_admin',
array(
'nonce' => wp_create_nonce( 'ewd-ufaq-wc-admin-js' )
)
);
}
public function faq_content() {
global $product;
global $ewd_ufaq_controller;
$product_post = ( $ewd_ufaq_controller->settings->get_setting( 'woocommerce-use-product' ) and is_object( $product ) ) ? get_post( $product->get_id() ) : get_post( get_the_id() );
$ufaq_product_category = get_term_by( 'name', $product_post->post_title, EWD_UFAQ_FAQ_CATEGORY_TAXONOMY );
echo '
' . esc_html( $ewd_ufaq_controller->settings->get_setting( 'label-woocommerce-tab' ) ) . '
';
$current_faqs = is_array( get_post_meta( $product_post->ID, 'EWD_UFAQ_WC_Selected_FAQs', true ) ) ? array_filter( get_post_meta( $product_post->ID, 'EWD_UFAQ_WC_Selected_FAQs', true ) ) : array();
if ( ! empty( $current_faqs ) ) {
$faq_list = implode( ',', $current_faqs );
echo do_shortcode( "[ultimate-faqs post__in_string='". sanitize_text_field( $faq_list ) . "']" );
}
else {
$wc_cats = get_the_terms( $product_post, 'product_cat' );
$ufaq_wc_category_list = '';
if ( $wc_cats ) {
foreach ( $wc_cats as $wc_cat ) {
$ufaq_wc_category = get_term_by( 'name', $wc_cat->name, EWD_UFAQ_FAQ_CATEGORY_TAXONOMY );
if ( $ufaq_wc_category ) { $ufaq_wc_category_list .= ',' . $ufaq_wc_category->slug; }
}
}
echo do_shortcode( "[ultimate-faqs include_category='". sanitize_text_field( $ufaq_product_category->slug . $ufaq_wc_category_list ) . "']" );
}
}
public function add_admin_product_page_faq_tab( $tabs ) {
global $ewd_ufaq_controller;
if ( ! $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs' ) ) { return $tabs; }
$args = array(
'label' => __('FAQs', 'ultimate-faqs'),
'target' => 'ewd_ufaq_faqs',
'class' => array()
);
$tabs['faqs'] = $args;
return $tabs;
}
public function add_admin_product_page_faqs() {
global $thepostid;
global $ewd_ufaq_controller;
if ( ! $ewd_ufaq_controller->settings->get_setting( 'woocommerce-faqs' ) ) { return $tabs; }
$current_faqs = (array) get_post_meta( $thepostid, 'EWD_UFAQ_WC_Selected_FAQs', true );
$all_faqs = get_posts( array( 'numberposts' => -1, 'post_type' => EWD_UFAQ_FAQ_POST_TYPE ) );
$categories = get_terms( array( 'taxonomy' => EWD_UFAQ_FAQ_CATEGORY_TAXONOMY ) );
?>
settings->get_setting( 'access-role' ) )
) {
ewdufaqHelper::admin_nopriv_ajax();
}
$post_id = intval( $_POST['Post_ID'] );
$current_faqs = (array) get_post_meta( $post_id, 'EWD_UFAQ_WC_Selected_FAQs', true );
$faqs = array_map( 'absint', json_decode( stripslashes_deep( $_POST['FAQs'] ) ) );
if ( ! is_array( $faqs ) ) { $faqs = array(); }
$added_faqs = array();
foreach ( $faqs as $faq ) {
if ( in_array( $faq, $current_faqs ) ) { continue; }
$current_faqs[] = $faq;
$faq_post = get_post( $faq );
$added_faqs[] = array( 'ID' => $faq, 'Name' => $faq_post->post_title);
}
update_post_meta( $post_id, 'EWD_UFAQ_WC_Selected_FAQs', array_filter( $current_faqs ) );
echo json_encode( $added_faqs );
die();
}
public function delete_wc_faqs() {
global $ewd_ufaq_controller;
if (
! check_ajax_referer( 'ewd-ufaq-wc-admin-js', 'nonce' )
||
! current_user_can( $ewd_ufaq_controller->settings->get_setting( 'access-role' ) )
) {
ewdufaqHelper::admin_nopriv_ajax();
}
$post_id = intval( $_POST['Post_ID'] );
$current_faqs = (array) get_post_meta( $post_id, 'EWD_UFAQ_WC_Selected_FAQs', true );
$faqs = array_map( 'absint', json_decode( stripslashes_deep( $_POST['FAQs'] ) ) );
if ( ! is_array( $faqs ) ) { $faqs = array(); }
$remaining_faqs = array_diff( $current_faqs, $faqs );
update_post_meta( $post_id, 'EWD_UFAQ_WC_Selected_FAQs', array_filter( $remaining_faqs ) );
die();
}
public function wc_faq_category() {
global $ewd_ufaq_controller;
if (
! check_ajax_referer( 'ewd-ufaq-wc-admin-js', 'nonce' )
||
! current_user_can( $ewd_ufaq_controller->settings->get_setting( 'access-role' ) )
) {
ewdufaqHelper::admin_nopriv_ajax();
}
$cat_id = intval( $_POST['Cat_ID'] );
$args = array(
'numberposts' => -1,
'post_type' => EWD_UFAQ_FAQ_POST_TYPE,
);
if ( $cat_id != '' ) {
$args['tax_query'] = array(
array(
'taxonomy' => EWD_UFAQ_FAQ_CATEGORY_TAXONOMY,
'terms' => $cat_id
)
);
}
$all_faqs = get_posts( $args );
ob_start();
?>