post_type ) {
wp_safe_redirect( admin_url( 'admin.php?page=bridgehub-sliders' ) );
exit;
}
$new_title = $original->post_title . ' — ' . __( 'Copy', 'aj-square-testimonial-slider' );
$new_id = wp_insert_post(
array(
'post_title' => $new_title,
'post_content' => $original->post_content,
'post_type' => 'bridgehub_slider',
'post_status' => 'draft',
)
);
if ( $new_id && ! is_wp_error( $new_id ) ) {
// Copy slider settings meta (skip testimonial order — rebuilt below).
$skip_keys = array(
'_wp_attachment_metadata',
'_wp_old_slug',
'_wp_old_id',
'bridgehub_testimonial_ids',
);
$meta = get_post_meta( $slider_id );
if ( is_array( $meta ) ) {
foreach ( $meta as $key => $values ) {
if ( in_array( $key, $skip_keys, true ) ) {
continue;
}
foreach ( $values as $value ) {
add_post_meta( $new_id, $key, $value );
}
}
}
// Duplicate child testimonials.
$old_ids = get_post_meta( $slider_id, 'bridgehub_testimonial_ids', true );
$new_testimonial_ids = array();
if ( is_array( $old_ids ) ) {
foreach ( $old_ids as $old_tid ) {
$old_post = get_post( $old_tid );
if ( ! $old_post || 'bhub_testimonial' !== $old_post->post_type ) {
continue;
}
$new_tid = wp_insert_post(
array(
'post_title' => $old_post->post_title,
'post_content' => $old_post->post_content,
'post_type' => 'bhub_testimonial',
'post_status' => $old_post->post_status,
'post_author' => get_current_user_id(),
)
);
if ( $new_tid && ! is_wp_error( $new_tid ) ) {
$testimonial_meta_keys = array(
'bridgehub_testimonial_designation',
'ajsqtesl_slider_subtitle',
'bridgehub_testimonial_company',
'bridgehub_testimonial_rating',
);
foreach ( $testimonial_meta_keys as $mk ) {
$mv = get_post_meta( $old_tid, $mk, true );
if ( '' !== $mv ) {
update_post_meta( $new_tid, $mk, $mv );
}
}
update_post_meta( $new_tid, 'bridgehub_testimonial_parent_slider', $new_id );
$thumb_id = get_post_thumbnail_id( $old_tid );
if ( $thumb_id ) {
set_post_thumbnail( $new_tid, $thumb_id );
}
$new_testimonial_ids[] = $new_tid;
}
}
}
update_post_meta( $new_id, 'bridgehub_testimonial_ids', $new_testimonial_ids );
if ( has_post_thumbnail( $slider_id ) ) {
set_post_thumbnail( $new_id, get_post_thumbnail_id( $slider_id ) );
}
wp_safe_redirect( admin_url( 'post.php?post=' . $new_id . '&action=edit&duplicated=1' ) );
exit;
}
wp_safe_redirect( admin_url( 'admin.php?page=bridgehub-sliders' ) );
exit;
}
public function enqueue_admin_assets( $hook ) {
$screen = get_current_screen();
$is_slider_list = ( false !== strpos( $hook, 'bridgehub-sliders' ) && 'post' !== $hook && 'post-new' !== $hook );
$is_slider_edit = ( $screen && 'bridgehub_slider' === $screen->post_type && in_array( $screen->base, array( 'post', 'post-new' ), true ) );
$is_doc_page = ( false !== strpos( $hook, 'bridgehub-documentation' ) );
if ( ! $is_slider_list && ! $is_slider_edit && ! $is_doc_page ) {
return;
}
$admin_css_path = BRIDGEHUB_PLUGIN_DIR . 'assets/css/bridgehub-admin.css';
$admin_css_url = BRIDGEHUB_PLUGIN_URL . 'assets/css/bridgehub-admin.css';
wp_enqueue_style(
'bridgehub-admin',
$admin_css_url,
array(),
file_exists( $admin_css_path ) ? filemtime( $admin_css_path ) : BRIDGEHUB_VERSION
);
if ( $is_slider_edit ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the current post ID on the admin edit screen; no data is modified.
$slider_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0;
wp_enqueue_media();
$admin_js_path = BRIDGEHUB_PLUGIN_DIR . 'assets/js/bridgehub-admin.js';
$admin_js_url = BRIDGEHUB_PLUGIN_URL . 'assets/js/bridgehub-admin.js';
wp_enqueue_script(
'bridgehub-admin',
$admin_js_url,
array( 'jquery', 'jquery-ui-sortable' ),
file_exists( $admin_js_path ) ? filemtime( $admin_js_path ) : BRIDGEHUB_VERSION,
true
);
wp_localize_script(
'bridgehub-admin',
'bridgehubAdmin',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'bridgehub_testimonial_action' ),
'sliderId' => $slider_id,
'copySuccess' => __( 'Copied!', 'aj-square-testimonial-slider' ),
'confirmDelete' => __( 'Are you sure you want to delete this slider? This cannot be undone.', 'aj-square-testimonial-slider' ),
'confirmDeleteTestimonial' => __( 'Are you sure you want to delete this testimonial? This cannot be undone.', 'aj-square-testimonial-slider' ),
'i18n' => array(
'addTestimonial' => __( 'Add Testimonial', 'aj-square-testimonial-slider' ),
'editTestimonial' => __( 'Edit Testimonial', 'aj-square-testimonial-slider' ),
'clientName' => __( 'Client Name', 'aj-square-testimonial-slider' ),
'testimonialContent' => __( 'Testimonial Content', 'aj-square-testimonial-slider' ),
'designation' => __( 'Designation', 'aj-square-testimonial-slider' ),
'company' => __( 'Company', 'aj-square-testimonial-slider' ),
'rating' => __( 'Rating', 'aj-square-testimonial-slider' ),
'featuredImage' => __( 'Featured Image', 'aj-square-testimonial-slider' ),
'chooseImage' => __( 'Choose Image', 'aj-square-testimonial-slider' ),
'removeImage' => __( 'Remove Image', 'aj-square-testimonial-slider' ),
'save' => __( 'Save Testimonial', 'aj-square-testimonial-slider' ),
'cancel' => __( 'Cancel', 'aj-square-testimonial-slider' ),
'saving' => __( 'Saving...', 'aj-square-testimonial-slider' ),
'saved' => __( 'Saved!', 'aj-square-testimonial-slider' ),
'noTestimonials' => __( 'No testimonials yet. Click "Add Testimonial" to create your first one.', 'aj-square-testimonial-slider' ),
'confirmDeleteTestimonial' => __( 'Are you sure you want to delete this testimonial?', 'aj-square-testimonial-slider' ),
'copyShortcode' => __( 'Copy Shortcode', 'aj-square-testimonial-slider' ),
'copied' => __( 'Copied!', 'aj-square-testimonial-slider' ),
'noRating' => __( 'No Rating', 'aj-square-testimonial-slider' ),
),
)
);
}
}
public function enqueue_frontend_assets() {
$has_slider = false;
if ( is_singular() ) {
global $post;
if ( is_a( $post, 'WP_Post' ) ) {
if ( has_shortcode( $post->post_content, 'bridgehub_testimonial_slider' ) || has_shortcode( $post->post_content, 'ajsqtesl_slider' ) ) {
$has_slider = true;
}
}
}
if ( ! $has_slider && ! is_customize_preview() ) {
return;
}
$style_path = BRIDGEHUB_PLUGIN_DIR . 'assets/css/ajsqtesl-style.css';
$style_url = BRIDGEHUB_PLUGIN_URL . 'assets/css/ajsqtesl-style.css';
wp_enqueue_style(
'ajsqtesl-style',
$style_url,
array(),
file_exists( $style_path ) ? filemtime( $style_path ) : BRIDGEHUB_VERSION
);
$js_path = BRIDGEHUB_PLUGIN_DIR . 'assets/js/ajsqtesl-plugin.js';
$js_url = BRIDGEHUB_PLUGIN_URL . 'assets/js/ajsqtesl-plugin.js';
wp_enqueue_script(
'ajsqtesl-plugin',
$js_url,
array( 'jquery' ),
file_exists( $js_path ) ? filemtime( $js_path ) : BRIDGEHUB_VERSION,
true
);
}
public function add_admin_body_class( $classes ) {
$screen = get_current_screen();
if ( $screen && 'bridgehub_slider' === $screen->post_type && in_array( $screen->base, array( 'post', 'post-new' ), true ) ) {
$classes .= ' bridgehub-slider-edit ';
}
return $classes;
}
public function render_all_sliders_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'aj-square-testimonial-slider' ) );
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading query parameters only to display admin notices.
$deleted = isset( $_GET['deleted'] );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading query parameters only to display admin notices.
$duplicated = isset( $_GET['duplicated'] );
$sliders = get_posts(
array(
'post_type' => 'bridgehub_slider',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'modified',
'order' => 'DESC',
)
);
?>
|
|
|
|
|
ID );
$edit_url = get_edit_post_link( $slider->ID, 'raw' );
$delete_url = wp_nonce_url(
admin_url( 'admin.php?page=bridgehub-sliders&bridgehub_delete_slider=' . $slider->ID ),
'bridgehub_delete_slider_' . $slider->ID
);
$duplicate_url = wp_nonce_url(
admin_url( 'admin.php?page=bridgehub-sliders&bridgehub_duplicate_slider=' . $slider->ID ),
'bridgehub_duplicate_slider_' . $slider->ID
);
$status = get_post_status( $slider );
$status_label = get_post_status_object( $status ) ? get_post_status_object( $status )->label : $status;
$is_published = ( 'publish' === $status );
?>
|
post_title ? $slider->post_title : __( '(Untitled)', 'aj-square-testimonial-slider' ) ); ?>
|
|
|
ID ) ); ?>
|
|
__( 'Permission denied.', 'aj-square-testimonial-slider' ) ) );
}
$slider_id = absint( $_POST['slider_id'] ?? 0 );
$testimonial_id = absint( $_POST['testimonial_id'] ?? 0 );
$client_name = sanitize_text_field( wp_unslash( $_POST['client_name'] ?? '' ) );
$content = wp_kses_post( wp_unslash( $_POST['content'] ?? '' ) );
$designation = sanitize_text_field( wp_unslash( $_POST['designation'] ?? '' ) );
$company = sanitize_text_field( wp_unslash( $_POST['company'] ?? '' ) );
$rating = absint( $_POST['rating'] ?? 0 );
$image_id = absint( $_POST['image_id'] ?? 0 );
if ( ! $slider_id ) {
wp_send_json_error( array( 'message' => __( 'Invalid slider ID.', 'aj-square-testimonial-slider' ) ) );
}
$slider_post = get_post( $slider_id );
if ( ! $slider_post || 'bridgehub_slider' !== $slider_post->post_type ) {
wp_send_json_error( array( 'message' => __( 'Invalid slider.', 'aj-square-testimonial-slider' ) ) );
}
if ( '' === $client_name ) {
wp_send_json_error( array( 'message' => __( 'Client name is required.', 'aj-square-testimonial-slider' ) ) );
}
$is_new = ( $testimonial_id <= 0 );
if ( $is_new ) {
$testimonial_id = wp_insert_post(
array(
'post_title' => $client_name,
'post_content' => $content,
'post_type' => 'bhub_testimonial',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
)
);
if ( is_wp_error( $testimonial_id ) ) {
wp_send_json_error( array( 'message' => $testimonial_id->get_error_message() ) );
}
// Add to slider's testimonial list.
$ids = get_post_meta( $slider_id, 'bridgehub_testimonial_ids', true );
if ( ! is_array( $ids ) ) {
$ids = array();
}
$ids[] = $testimonial_id;
update_post_meta( $slider_id, 'bridgehub_testimonial_ids', array_unique( $ids ) );
} else {
$existing = get_post( $testimonial_id );
if ( ! $existing || 'bhub_testimonial' !== $existing->post_type ) {
wp_send_json_error( array( 'message' => __( 'Invalid testimonial.', 'aj-square-testimonial-slider' ) ) );
}
wp_update_post(
array(
'ID' => $testimonial_id,
'post_title' => $client_name,
'post_content' => $content,
)
);
}
// Save meta.
update_post_meta( $testimonial_id, 'bridgehub_testimonial_designation', $designation );
update_post_meta( $testimonial_id, 'ajsqtesl_slider_subtitle', $designation );
update_post_meta( $testimonial_id, 'bridgehub_testimonial_company', $company );
update_post_meta( $testimonial_id, 'bridgehub_testimonial_parent_slider', $slider_id );
if ( $rating >= 1 && $rating <= 5 ) {
update_post_meta( $testimonial_id, 'bridgehub_testimonial_rating', $rating );
} else {
delete_post_meta( $testimonial_id, 'bridgehub_testimonial_rating' );
}
if ( $image_id > 0 ) {
set_post_thumbnail( $testimonial_id, $image_id );
} else {
delete_post_thumbnail( $testimonial_id );
}
// Build the HTML for the updated item.
$html = $this->get_testimonial_item_html( $testimonial_id, $slider_id );
wp_send_json_success(
array(
'message' => $is_new ? __( 'Testimonial added.', 'aj-square-testimonial-slider' ) : __( 'Testimonial updated.', 'aj-square-testimonial-slider' ),
'id' => $testimonial_id,
'html' => $html,
'is_new' => $is_new,
'count' => count( get_post_meta( $slider_id, 'bridgehub_testimonial_ids', true ) ),
)
);
}
/**
* AJAX: Delete a testimonial.
*/
public function ajax_delete_testimonial() {
check_ajax_referer( 'bridgehub_testimonial_action', 'nonce' );
if ( ! current_user_can( 'edit_posts' ) ) {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aj-square-testimonial-slider' ) ) );
}
$testimonial_id = absint( $_POST['testimonial_id'] ?? 0 );
$slider_id = absint( $_POST['slider_id'] ?? 0 );
if ( ! $testimonial_id || ! $slider_id ) {
wp_send_json_error( array( 'message' => __( 'Invalid parameters.', 'aj-square-testimonial-slider' ) ) );
}
// Remove from slider's testimonial list.
$ids = get_post_meta( $slider_id, 'bridgehub_testimonial_ids', true );
if ( is_array( $ids ) ) {
$ids = array_diff( $ids, array( $testimonial_id ) );
update_post_meta( $slider_id, 'bridgehub_testimonial_ids', array_values( $ids ) );
}
wp_delete_post( $testimonial_id, true );
wp_send_json_success(
array(
'message' => __( 'Testimonial deleted.', 'aj-square-testimonial-slider' ),
'count' => count( $ids ?? array() ),
)
);
}
/**
* AJAX: Duplicate a testimonial.
*/
public function ajax_duplicate_testimonial() {
check_ajax_referer( 'bridgehub_testimonial_action', 'nonce' );
if ( ! current_user_can( 'edit_posts' ) ) {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aj-square-testimonial-slider' ) ) );
}
$testimonial_id = absint( $_POST['testimonial_id'] ?? 0 );
$slider_id = absint( $_POST['slider_id'] ?? 0 );
if ( ! $testimonial_id || ! $slider_id ) {
wp_send_json_error( array( 'message' => __( 'Invalid parameters.', 'aj-square-testimonial-slider' ) ) );
}
$original = get_post( $testimonial_id );
if ( ! $original || 'bhub_testimonial' !== $original->post_type ) {
wp_send_json_error( array( 'message' => __( 'Invalid testimonial.', 'aj-square-testimonial-slider' ) ) );
}
$new_id = wp_insert_post(
array(
'post_title' => $original->post_title . ' — ' . __( 'Copy', 'aj-square-testimonial-slider' ),
'post_content' => $original->post_content,
'post_type' => 'bhub_testimonial',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
)
);
if ( is_wp_error( $new_id ) ) {
wp_send_json_error( array( 'message' => $new_id->get_error_message() ) );
}
// Copy meta.
$meta_keys = array(
'bridgehub_testimonial_designation',
'ajsqtesl_slider_subtitle',
'bridgehub_testimonial_company',
'bridgehub_testimonial_rating',
);
foreach ( $meta_keys as $key ) {
$value = get_post_meta( $testimonial_id, $key, true );
if ( '' !== $value ) {
update_post_meta( $new_id, $key, $value );
}
}
update_post_meta( $new_id, 'bridgehub_testimonial_parent_slider', $slider_id );
// Copy featured image.
$thumb_id = get_post_thumbnail_id( $testimonial_id );
if ( $thumb_id ) {
set_post_thumbnail( $new_id, $thumb_id );
}
// Add to slider's list (after the original).
$ids = get_post_meta( $slider_id, 'bridgehub_testimonial_ids', true );
if ( ! is_array( $ids ) ) {
$ids = array();
}
$pos = array_search( $testimonial_id, $ids );
$pos = false !== $pos ? $pos + 1 : count( $ids );
array_splice( $ids, $pos, 0, array( $new_id ) );
update_post_meta( $slider_id, 'bridgehub_testimonial_ids', $ids );
$html = $this->get_testimonial_item_html( $new_id, $slider_id );
wp_send_json_success(
array(
'message' => __( 'Testimonial duplicated.', 'aj-square-testimonial-slider' ),
'id' => $new_id,
'html' => $html,
'count' => count( $ids ),
)
);
}
/**
* AJAX: Reorder testimonials.
*/
public function ajax_reorder_testimonials() {
check_ajax_referer( 'bridgehub_testimonial_action', 'nonce' );
if ( ! current_user_can( 'edit_posts' ) ) {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aj-square-testimonial-slider' ) ) );
}
$slider_id = absint( $_POST['slider_id'] ?? 0 );
$order = isset( $_POST['order'] ) ? array_map( 'absint', (array) $_POST['order'] ) : array();
if ( ! $slider_id || empty( $order ) ) {
wp_send_json_error( array( 'message' => __( 'Invalid parameters.', 'aj-square-testimonial-slider' ) ) );
}
update_post_meta( $slider_id, 'bridgehub_testimonial_ids', array_unique( $order ) );
wp_send_json_success( array( 'message' => __( 'Order updated.', 'aj-square-testimonial-slider' ) ) );
}
/**
* AJAX: Get full testimonial data for editing.
*/
public function ajax_get_testimonial_data() {
check_ajax_referer( 'bridgehub_testimonial_action', 'nonce' );
if ( ! current_user_can( 'edit_posts' ) ) {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'aj-square-testimonial-slider' ) ) );
}
$testimonial_id = absint( $_POST['testimonial_id'] ?? 0 );
if ( ! $testimonial_id ) {
wp_send_json_error( array( 'message' => __( 'Invalid testimonial ID.', 'aj-square-testimonial-slider' ) ) );
}
$post = get_post( $testimonial_id );
if ( ! $post || 'bhub_testimonial' !== $post->post_type ) {
wp_send_json_error( array( 'message' => __( 'Invalid testimonial.', 'aj-square-testimonial-slider' ) ) );
}
$thumb_id = get_post_thumbnail_id( $testimonial_id );
$thumb_url = $thumb_id ? wp_get_attachment_image_url( $thumb_id, 'medium' ) : '';
wp_send_json_success(
array(
'title' => $post->post_title,
'content' => $post->post_content,
'designation' => get_post_meta( $testimonial_id, 'bridgehub_testimonial_designation', true ),
'company' => get_post_meta( $testimonial_id, 'bridgehub_testimonial_company', true ),
'rating' => absint( get_post_meta( $testimonial_id, 'bridgehub_testimonial_rating', true ) ),
'image_id' => $thumb_id,
'image_url' => $thumb_url,
)
);
}
/**
* Build the HTML for a single testimonial list item.
*/
private function get_testimonial_item_html( $testimonial_id, $slider_id ) {
$testimonial = get_post( $testimonial_id );
if ( ! $testimonial ) {
return '';
}
$thumb_id = get_post_thumbnail_id( $testimonial_id );
$thumb_url = $thumb_id ? wp_get_attachment_image_url( $thumb_id, 'thumbnail' ) : '';
$designation = get_post_meta( $testimonial_id, 'bridgehub_testimonial_designation', true );
if ( '' === $designation ) {
$designation = get_post_meta( $testimonial_id, 'ajsqtesl_slider_subtitle', true );
}
$company = get_post_meta( $testimonial_id, 'bridgehub_testimonial_company', true );
$rating = absint( get_post_meta( $testimonial_id, 'bridgehub_testimonial_rating', true ) );
$excerpt = wp_trim_words( $testimonial->post_content, 15 );
ob_start();
?>
post_title ? $testimonial->post_title : __( '(Untitled)', 'aj-square-testimonial-slider' ) ); ?>
,
0 ) : ?>
★' : '☆';
}
?>
[bridgehub_testimonial_slider id="123"] —
[ajsqtesl_slider] —