| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin action file. |
| 4 |
* |
| 5 |
* @package Smart_Post_Show |
| 6 |
* @subpackage Smart_Post_Show/admin/views |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! function_exists( 'pcp_get_option' ) ) { |
| 14 |
/** |
| 15 |
* The pcp_get_option function. |
| 16 |
* |
| 17 |
* @param string $option The option unique ID. |
| 18 |
* @param mixed $default The default value for the option. |
| 19 |
* @return statement |
| 20 |
*/ |
| 21 |
function pcp_get_option( $option = '', $default = null ) { |
| 22 |
$options = get_option( 'sp_pcp_settings' ); |
| 23 |
return ( isset( $options[ $option ] ) ) ? $options[ $option ] : $default; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Populate the taxonomy name list to the select option. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
function sps_get_taxonomies() { |
| 33 |
// Check for nonce security. |
| 34 |
$nonce = ( ! empty( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 35 |
// Check for nonce security. |
| 36 |
if ( wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) { |
| 37 |
$capability = apply_filters( 'sp_pc_dashboard_capability', 'manage_options' ); |
| 38 |
if ( current_user_can( $capability ) ) { |
| 39 |
$the_pcp_post_types = ( ! empty( $_POST['pcp_post_types'] ) ) ? sanitize_text_field( wp_unslash( $_POST['pcp_post_types'] ) ) : ''; |
| 40 |
$sp_post_types = $the_pcp_post_types ? $the_pcp_post_types : get_post_types( array(), 'names' ); |
| 41 |
|
| 42 |
$taxonomy_names = get_object_taxonomies( $sp_post_types, 'names' ); |
| 43 |
if ( ! is_wp_error( $taxonomy_names ) && ! empty( $taxonomy_names ) ) { |
| 44 |
echo '<option value="">' . esc_html__( 'Select Taxonomy', 'post-carousel' ) . '</option>'; |
| 45 |
foreach ( $taxonomy_names as $taxonomy => $label ) { |
| 46 |
echo '<option value="' . esc_attr( $label ) . '">' . esc_html( $label ) . '</option>'; |
| 47 |
} |
| 48 |
} |
| 49 |
} else { |
| 50 |
wp_send_json_error( array( 'error' => esc_html__( 'You do not have required permissions to access.', 'post-carousel' ) ) ); |
| 51 |
} |
| 52 |
} else { |
| 53 |
wp_send_json_error( array( 'error' => esc_html__( 'Error: Nonce verification has failed. Please try again.', 'post-carousel' ) ) ); |
| 54 |
} |
| 55 |
} |
| 56 |
add_action( 'wp_ajax_sps_get_taxonomies', 'sps_get_taxonomies' ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Populate the taxonomy terms list to the select option. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
function sps_get_terms() { |
| 64 |
$nonce = ( ! empty( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 65 |
// Check for nonce security. |
| 66 |
if ( wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) { |
| 67 |
$capability = apply_filters( 'sp_pc_dashboard_capability', 'manage_options' ); |
| 68 |
if ( current_user_can( $capability ) ) { |
| 69 |
$the_pcp_taxonomy = ( ! empty( $_POST['pcp_post_taxonomy'] ) ) ? sanitize_text_field( wp_unslash( $_POST['pcp_post_taxonomy'] ) ) : ''; |
| 70 |
$sp_post_types = get_post_types( array(), 'names' ); |
| 71 |
$pcp_taxonomy = $the_pcp_taxonomy ? $the_pcp_taxonomy : get_object_taxonomies( $sp_post_types, 'names' ); |
| 72 |
if ( version_compare( get_bloginfo( 'version' ), '4.5', '>=' ) ) { |
| 73 |
$terms = get_terms( array( 'taxonomy' => $pcp_taxonomy ) ); |
| 74 |
} else { |
| 75 |
$terms = get_terms( array( $pcp_taxonomy ) ); |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $terms as $key => $value ) { |
| 79 |
echo '<option value="' . esc_attr( $value->term_id ) . '">' . esc_html( $value->name ) . '</option>'; |
| 80 |
} |
| 81 |
} else { |
| 82 |
wp_send_json_error( array( 'error' => esc_html__( 'You do not have required permissions to access.', 'post-carousel' ) ) ); |
| 83 |
} |
| 84 |
} else { |
| 85 |
wp_send_json_error( array( 'error' => esc_html__( 'Error: Nonce verification has failed. Please try again.', 'post-carousel' ) ) ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
add_action( 'wp_ajax_sps_get_terms', 'sps_get_terms' ); |
| 90 |
|
| 91 |
if ( ! function_exists( 'spf_chosen_ajax' ) ) { |
| 92 |
/** |
| 93 |
* |
| 94 |
* Chosen Ajax |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* @version 1.0.0 |
| 98 |
*/ |
| 99 |
function spf_chosen_ajax() { |
| 100 |
|
| 101 |
$nonce = ( ! empty( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 102 |
$type = ( ! empty( $_POST['type'] ) ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; |
| 103 |
$term = ( ! empty( $_POST['term'] ) ) ? sanitize_text_field( wp_unslash( $_POST['term'] ) ) : ''; |
| 104 |
$query = ( ! empty( $_POST['query_args'] ) ) ? wp_kses_post_deep( wp_unslash( $_POST['query_args'] ) ) : array(); // phpcs:ignore |
| 105 |
if ( wp_verify_nonce( $nonce, 'spf_chosen_ajax_nonce' ) ) { |
| 106 |
$capability = apply_filters( 'spf_chosen_ajax_capability', 'manage_options' ); |
| 107 |
if ( current_user_can( $capability ) ) { |
| 108 |
$options = SP_PC_Fields::field_data( $type, $term, $query ); |
| 109 |
wp_send_json_success( $options ); |
| 110 |
} else { |
| 111 |
wp_send_json_error( array( 'error' => esc_html__( 'You do not have required permissions to access.', 'post-carousel' ) ) ); |
| 112 |
} |
| 113 |
} else { |
| 114 |
wp_send_json_error( array( 'error' => esc_html__( 'Error: Nonce verification has failed. Please try again.', 'post-carousel' ) ) ); |
| 115 |
} |
| 116 |
} |
| 117 |
add_action( 'wp_ajax_spf-chosen', 'spf_chosen_ajax' ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! function_exists( 'spf_set_icons' ) ) { |
| 121 |
/** |
| 122 |
* |
| 123 |
* Set icons for wp dialog |
| 124 |
* |
| 125 |
* @since 1.0.0 |
| 126 |
* @version 1.0.0 |
| 127 |
*/ |
| 128 |
function spf_set_icons() { |
| 129 |
global $post_type; |
| 130 |
if ( 'sp_post_carousel' === $post_type ) { |
| 131 |
?> |
| 132 |
<div id="spf-modal-icon" class="spf-modal spf-modal-icon"> |
| 133 |
<div class="spf-modal-table"> |
| 134 |
<div class="spf-modal-table-cell"> |
| 135 |
<div class="spf-modal-overlay"></div> |
| 136 |
<div class="spf-modal-inner"> |
| 137 |
<div class="spf-modal-title"> |
| 138 |
<?php esc_html_e( 'Add Icon', 'post-carousel' ); ?> |
| 139 |
<div class="spf-modal-close spf-icon-close"></div> |
| 140 |
</div> |
| 141 |
<div class="spf-modal-header spf-text-center"> |
| 142 |
<input type="text" placeholder="<?php esc_html_e( 'Search a Icon...', 'post-carousel' ); ?>" class="spf-icon-search" /> |
| 143 |
</div> |
| 144 |
<div class="spf-modal-content"> |
| 145 |
<div class="spf-modal-loading"><div class="spf-loading"></div></div> |
| 146 |
<div class="spf-modal-load"></div> |
| 147 |
</div> |
| 148 |
</div> |
| 149 |
</div> |
| 150 |
</div> |
| 151 |
</div> |
| 152 |
<?php |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
add_action( 'admin_footer', 'spf_set_icons' ); |
| 157 |
add_action( 'customize_controls_print_footer_scripts', 'spf_set_icons' ); |
| 158 |
} |
| 159 |
|