| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax requests handler |
| 4 |
* |
| 5 |
* @package Woody_Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get snippet library table content |
| 15 |
*/ |
| 16 |
function wbcr_inp_ajax_get_snippet_library() { |
| 17 |
if ( ! WINP_Plugin::app()->current_user_car() ) { |
| 18 |
wp_die( - 1, 403 ); |
| 19 |
} |
| 20 |
|
| 21 |
check_ajax_referer( 'winp-snippet-library', 'winp_nonce' ); |
| 22 |
?> |
| 23 |
<div class="wrap"> |
| 24 |
<form id="winp-snippet-library-list" method="get"> |
| 25 |
<input type="hidden" name="page" value="<?php echo WINP_HTTP::request( 'page', 1, true ); ?>"/> |
| 26 |
<input type="hidden" name="order" value="<?php echo WINP_HTTP::request( 'order', 'asc', true ); ?>"/> |
| 27 |
<input type="hidden" name="orderby" value="<?php echo WINP_HTTP::request( 'orderby', 'title', true ); ?>"/> |
| 28 |
<div id="winp-snippet-library-table" style=""> |
| 29 |
<p><?php _e( 'Loading snippets...', 'insert-php' ); ?></p> |
| 30 |
<?php |
| 31 |
wp_nonce_field( 'winp-ajax-custom-list-nonce', 'winp_ajax_custom_list_nonce' ); |
| 32 |
?> |
| 33 |
</div> |
| 34 |
</form> |
| 35 |
</div> |
| 36 |
<?php |
| 37 |
wp_die(); |
| 38 |
} |
| 39 |
|
| 40 |
add_action( 'wp_ajax_winp_get_snippet_library', 'wbcr_inp_ajax_get_snippet_library' ); |
| 41 |
|
| 42 |
/** |
| 43 |
* Snippet create from library |
| 44 |
*/ |
| 45 |
function wbcr_inp_ajax_snippet_create() { |
| 46 |
if ( ! WINP_Plugin::app()->current_user_car() ) { |
| 47 |
wp_die( - 1, 403 ); |
| 48 |
} |
| 49 |
|
| 50 |
check_ajax_referer( 'winp-ajax-custom-list-nonce', 'winp_ajax_custom_list_nonce' ); |
| 51 |
|
| 52 |
$snippet_id = WINP_HTTP::post( 'snippet_id', 0, true ); |
| 53 |
$post_id = WINP_HTTP::post( 'post_id', 0, true ); |
| 54 |
$common = WINP_HTTP::post( 'common', 0 ); |
| 55 |
$result = WINP_Plugin::app()->get_api_object()->create_from_library( $snippet_id, $post_id, $common ); |
| 56 |
|
| 57 |
echo( $result ); |
| 58 |
exit(); |
| 59 |
} |
| 60 |
|
| 61 |
add_action( 'wp_ajax_winp_snippet_create', 'wbcr_inp_ajax_snippet_create' ); |
| 62 |
|
| 63 |
/** |
| 64 |
* Snippet delete from library |
| 65 |
*/ |
| 66 |
function wbcr_inp_ajax_snippet_delete() { |
| 67 |
if ( ! WINP_Plugin::app()->current_user_car() ) { |
| 68 |
wp_die( - 1, 403 ); |
| 69 |
} |
| 70 |
|
| 71 |
$snippet_id = WINP_HTTP::post( 'snippet_id', 0, true ); |
| 72 |
|
| 73 |
check_ajax_referer( 'winp-ajax-snippet-delete-' . $snippet_id, 'winp_ajax_snippet_delete_nonce' ); |
| 74 |
|
| 75 |
$result = WINP_Plugin::app()->get_api_object()->delete_snippet( $snippet_id ); |
| 76 |
|
| 77 |
echo( $result ); |
| 78 |
exit(); |
| 79 |
} |
| 80 |
|
| 81 |
add_action( 'wp_ajax_winp_snippet_delete', 'wbcr_inp_ajax_snippet_delete' ); |
| 82 |
|
| 83 |
/** |
| 84 |
* Action wp_ajax for fetching the first time table structure |
| 85 |
*/ |
| 86 |
function wbcr_inp_ajax_sts_display_callback() { |
| 87 |
if ( ! WINP_Plugin::app()->current_user_car() ) { |
| 88 |
wp_die( - 1, 403 ); |
| 89 |
} |
| 90 |
|
| 91 |
check_ajax_referer( 'winp-ajax-custom-list-nonce', 'winp_ajax_custom_list_nonce' ); |
| 92 |
|
| 93 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.snippets.table.php'; |
| 94 |
|
| 95 |
// Create an instance of our package class... |
| 96 |
$snippet_list_table = new WINP_Snippet_Library_Table( true ); |
| 97 |
// Fetch, prepare, sort, and filter our data... |
| 98 |
$snippet_list_table->prepare_items(); |
| 99 |
ob_start(); |
| 100 |
$snippet_list_table->display(); |
| 101 |
$display = ob_get_clean(); |
| 102 |
die( |
| 103 |
json_encode( |
| 104 |
[ |
| 105 |
'display' => $display, |
| 106 |
] |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
add_action( 'wp_ajax_winp_sts_display', 'wbcr_inp_ajax_sts_display_callback' ); |
| 112 |
|
| 113 |
|
| 114 |
|