| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\ShortLinksPro\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Overrides GamiPress AJAX Helper for selecting posts |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
function gamipress_shortlinkspro_ajax_get_posts() { |
| 18 |
|
| 19 |
// Security check, forces to die if not security passed |
| 20 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 21 |
|
| 22 |
// Check if user can manage GamiPress |
| 23 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 24 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'slp_links', $_REQUEST['post_type'] ) ) { |
| 30 |
|
| 31 |
$results = array(); |
| 32 |
|
| 33 |
// Pull back the search string |
| 34 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 35 |
|
| 36 |
// Setup table |
| 37 |
$ct_table = ct_setup_table( 'shortlinkspro_links' ); |
| 38 |
|
| 39 |
// Get the links |
| 40 |
$links = $wpdb->get_results( $wpdb->prepare( |
| 41 |
"SELECT s.id, s.title |
| 42 |
FROM {$ct_table->db->table_name} AS s |
| 43 |
WHERE s.title LIKE %s", |
| 44 |
"%%{$search}%%" |
| 45 |
) ); |
| 46 |
|
| 47 |
ct_reset_setup_table(); |
| 48 |
|
| 49 |
foreach ( $links as $link ) { |
| 50 |
|
| 51 |
// Results should meet same structure like posts |
| 52 |
$results[] = array( |
| 53 |
'ID' => $link->id, |
| 54 |
'post_title' => ! empty( $link->title ) ? $link->title : '(no title)', |
| 55 |
); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
// Return our results |
| 60 |
wp_send_json_success( $results ); |
| 61 |
die; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_shortlinkspro_ajax_get_posts', 5 ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Get link categories |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
function gamipress_shortlinkspro_get_link_categories( ) { |
| 76 |
|
| 77 |
global $wpdb; |
| 78 |
|
| 79 |
// Setup table |
| 80 |
$ct_table = ct_setup_table( 'shortlinkspro_link_categories' ); |
| 81 |
|
| 82 |
// Query search |
| 83 |
$categories = $wpdb->get_results( |
| 84 |
"SELECT c.id, c.name |
| 85 |
FROM {$ct_table->db->table_name} AS c" |
| 86 |
); |
| 87 |
|
| 88 |
ct_reset_setup_table(); |
| 89 |
|
| 90 |
return $categories; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get link tags |
| 96 |
* |
| 97 |
* @since 1.0.0 |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
function gamipress_shortlinkspro_get_link_tags( ) { |
| 102 |
|
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
// Setup table |
| 106 |
$ct_table = ct_setup_table( 'shortlinkspro_link_tags' ); |
| 107 |
|
| 108 |
// Query search |
| 109 |
$tags = $wpdb->get_results( |
| 110 |
"SELECT t.id, t.name |
| 111 |
FROM {$ct_table->db->table_name} AS t" |
| 112 |
); |
| 113 |
|
| 114 |
ct_reset_setup_table(); |
| 115 |
|
| 116 |
return $tags; |
| 117 |
|
| 118 |
} |