| 1 |
<?php |
| 2 |
|
| 3 |
class SimpleTags_Admin_Autocomplete { |
| 4 |
|
| 5 |
public function __construct() { |
| 6 |
// Ajax action, JS Helper and admin action |
| 7 |
add_action( 'wp_ajax_simpletags', array( __CLASS__, 'ajax_check' ) ); |
| 8 |
|
| 9 |
// TaxoPress hook |
| 10 |
add_action( 'simpletags-auto_terms', array( __CLASS__, 'auto_terms_js' ) ); |
| 11 |
add_action( 'simpletags-manage_terms', array( __CLASS__, 'manage_terms_js' ) ); |
| 12 |
add_action( 'simpletags-mass_terms', array( __CLASS__, 'mass_terms_js' ) ); |
| 13 |
|
| 14 |
// Javascript |
| 15 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ), 11 ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Init some JS and CSS need for this feature |
| 20 |
* |
| 21 |
* @return void |
| 22 |
* @author WebFactory Ltd |
| 23 |
*/ |
| 24 |
public static function admin_enqueue_scripts() { |
| 25 |
global $pagenow; |
| 26 |
|
| 27 |
// Register JS/CSS |
| 28 |
wp_register_script( |
| 29 |
'st-helper-autocomplete', |
| 30 |
STAGS_URL . '/assets/js/helper-autocomplete.js', |
| 31 |
array( |
| 32 |
'jquery', |
| 33 |
'jquery-ui-autocomplete', |
| 34 |
), |
| 35 |
STAGS_VERSION |
| 36 |
); |
| 37 |
|
| 38 |
// Declare locations |
| 39 |
$wp_post_pages = array( 'post.php', 'post-new.php' ); |
| 40 |
$wp_page_pages = array( 'page.php', 'page-new.php' ); |
| 41 |
$st_pages = array( 'st_autoterms', 'st_mass_terms', 'st_manage' ); |
| 42 |
|
| 43 |
// Helper for posts/pages and for Auto Tags, Mass Edit Tags and Manage tags ! |
| 44 |
if ( ( in_array( $pagenow, $wp_post_pages, true ) || ( in_array( $pagenow, $wp_page_pages, true ) && is_page_have_tags() ) ) || ( isset( $_GET['page'] ) && in_array( $_GET['page'], $st_pages, true ) ) ) { |
| 45 |
wp_enqueue_script( 'st-helper-autocomplete' ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Ajax Dispatcher |
| 51 |
* |
| 52 |
*/ |
| 53 |
public static function ajax_check() { |
| 54 |
if ( isset( $_GET['stags_action'] ) && 'helper_js_collection' === $_GET['stags_action'] ) { |
| 55 |
self::ajax_local_tags(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Display a javascript collection for autocompletion script ! |
| 61 |
* |
| 62 |
* @return void |
| 63 |
* @author WebFactory Ltd |
| 64 |
*/ |
| 65 |
public static function ajax_local_tags() { |
| 66 |
status_header( 200 ); // Send good header HTTP |
| 67 |
header( 'Content-Type: application/json; charset=' . get_bloginfo( 'charset' ) ); |
| 68 |
|
| 69 |
$taxonomy = 'post_tag'; |
| 70 |
if ( isset( $_REQUEST['taxonomy'] ) && taxonomy_exists( sanitize_text_field($_REQUEST['taxonomy']) ) ) { |
| 71 |
$taxonomy = sanitize_text_field($_REQUEST['taxonomy']); |
| 72 |
} |
| 73 |
|
| 74 |
if ( (int) wp_count_terms( $taxonomy, array( 'hide_empty' => false ) ) === 0 ) { // No tags to suggest |
| 75 |
echo wp_json_encode( array() ); |
| 76 |
exit(); |
| 77 |
} |
| 78 |
|
| 79 |
// Prepare search |
| 80 |
$search = ( isset( $_GET['term'] ) ) ? trim( stripslashes( sanitize_text_field($_GET['term']) )) : ''; |
| 81 |
|
| 82 |
// Get all terms, or filter with search |
| 83 |
$terms = SimpleTags_Admin::getTermsForAjax( $taxonomy, $search ); |
| 84 |
if ( empty( $terms ) ) { |
| 85 |
echo wp_json_encode( array() ); |
| 86 |
exit(); |
| 87 |
} |
| 88 |
|
| 89 |
// Format |
| 90 |
$results = array(); |
| 91 |
foreach ( (array) $terms as $term ) { |
| 92 |
$term->name = stripslashes( $term->name ); |
| 93 |
$term->name = str_replace( array( "\r\n", "\r", "\n" ), '', $term->name ); |
| 94 |
|
| 95 |
$results[] = array( |
| 96 |
'id' => $term->term_id, |
| 97 |
'label' => $term->name, |
| 98 |
'value' => $term->name, |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
echo wp_json_encode( $results ); |
| 103 |
exit(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Content of custom meta box of TaxoPress |
| 108 |
* |
| 109 |
* @param object $post |
| 110 |
* |
| 111 |
* @return void |
| 112 |
* @author WebFactory Ltd |
| 113 |
*/ |
| 114 |
public static function metabox( $post ) { |
| 115 |
// Get options |
| 116 |
$autocomplete_min = 0 |
| 117 |
?> |
| 118 |
<p> |
| 119 |
<?php wp_nonce_field( 'update-simple-tags', 'nonce-simple-tags' ); ?> |
| 120 |
<input type="hidden" name="adv-tags-input-here" value="1"/> |
| 121 |
<input type="text" class="widefat" name="adv-tags-input" id="adv-tags-input" |
| 122 |
value="<?php echo esc_attr( SimpleTags_Admin::getTermsToEdit( 'post_tag', $post->ID ) ); ?>"/> |
| 123 |
|
| 124 |
<?php esc_html_e( 'Separate tags with commas', 'simple-tags' ); ?> |
| 125 |
</p> |
| 126 |
<script type="text/javascript"> |
| 127 |
<!-- |
| 128 |
st_init_autocomplete('#adv-tags-input', '<?php echo admin_url( "admin-ajax.php?action=simpletags&stags_action=helper_js_collection" ); ?>', <?php echo $autocomplete_min; ?>) |
| 129 |
--> |
| 130 |
</script> |
| 131 |
<?php |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* public static function called on auto terms page |
| 136 |
* |
| 137 |
* @param string $taxonomy |
| 138 |
* |
| 139 |
* @return void |
| 140 |
* @author WebFactory Ltd |
| 141 |
*/ |
| 142 |
public static function auto_terms_js( $taxonomy = '' ) { |
| 143 |
// Get option |
| 144 |
$autocomplete_min = 0 |
| 145 |
?> |
| 146 |
<script type="text/javascript"> |
| 147 |
<!-- |
| 148 |
st_init_autocomplete('#auto_list', "<?php echo admin_url( 'admin-ajax.php?action=simpletags&stags_action=helper_js_collection&taxonomy=' . $taxonomy ); ?>", <?php echo $autocomplete_min; ?>) |
| 149 |
--> |
| 150 |
</script> |
| 151 |
<?php |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* public static function called on manage terms page |
| 156 |
* |
| 157 |
* @param string $taxonomy |
| 158 |
* |
| 159 |
* @return void |
| 160 |
* @author WebFactory Ltd |
| 161 |
*/ |
| 162 |
public static function manage_terms_js( $taxonomy = '' ) { |
| 163 |
// Get option |
| 164 |
$autocomplete_min = 0 |
| 165 |
?> |
| 166 |
<script type="text/javascript"> |
| 167 |
<!-- |
| 168 |
st_init_autocomplete('.autocomplete-input', "<?php echo admin_url( 'admin-ajax.php?action=simpletags&stags_action=helper_js_collection&taxonomy=' . $taxonomy ); ?>", <?php echo $autocomplete_min; ?>) |
| 169 |
--> |
| 170 |
</script> |
| 171 |
<?php |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* public static function called on mass terms page |
| 176 |
* |
| 177 |
* @param string $taxonomy |
| 178 |
* |
| 179 |
* @return void |
| 180 |
* @author WebFactory Ltd |
| 181 |
*/ |
| 182 |
public static function mass_terms_js( $taxonomy = '' ) { |
| 183 |
// Get option |
| 184 |
$autocomplete_min = 0 |
| 185 |
?> |
| 186 |
<script type="text/javascript"> |
| 187 |
<!-- |
| 188 |
st_init_autocomplete('.autocomplete-input', "<?php echo admin_url( 'admin-ajax.php?action=simpletags&stags_action=helper_js_collection&taxonomy=' . $taxonomy ); ?>", <?php echo $autocomplete_min; ?>) |
| 189 |
--> |
| 190 |
</script> |
| 191 |
<?php |
| 192 |
} |
| 193 |
} |
| 194 |
|