| 1 |
<?php |
| 2 |
/** |
| 3 |
* AJAX class. |
| 4 |
* |
| 5 |
* @package WPZinc\Social |
| 6 |
* @author WP Zinc |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPZinc\Social; |
| 10 |
|
| 11 |
/** |
| 12 |
* Registers AJAX actions for saving statuses, fetching usernames, |
| 13 |
* searching Taxonomy Terms etc. |
| 14 |
* |
| 15 |
* @package WPZinc\Social |
| 16 |
* @author WP Zinc |
| 17 |
* @version 3.0.0 |
| 18 |
*/ |
| 19 |
class Ajax { |
| 20 |
|
| 21 |
/** |
| 22 |
* Holds the base class object. |
| 23 |
* |
| 24 |
* @since 3.4.7 |
| 25 |
* |
| 26 |
* @var object |
| 27 |
*/ |
| 28 |
public $base; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
* |
| 33 |
* @since 3.0.0 |
| 34 |
* |
| 35 |
* @param object $base Base Plugin Class. |
| 36 |
*/ |
| 37 |
public function __construct( $base ) { |
| 38 |
|
| 39 |
// Store base class. |
| 40 |
$this->base = $base; |
| 41 |
|
| 42 |
// Actions. |
| 43 |
add_action( 'wp_ajax_' . $this->base->plugin->filter_name . '_username_save_twitter', array( $this, 'username_save_twitter' ) ); |
| 44 |
add_action( 'wp_ajax_' . $this->base->plugin->filter_name . '_save_statuses', array( $this, 'save_statuses' ) ); |
| 45 |
add_action( 'wp_ajax_' . $this->base->plugin->filter_name . '_get_status_row', array( $this, 'get_status_row' ) ); |
| 46 |
add_action( 'wp_ajax_' . $this->base->plugin->filter_name . '_get_log', array( $this, 'get_log' ) ); |
| 47 |
add_action( 'wp_ajax_' . $this->base->plugin->filter_name . '_clear_log', array( $this, 'clear_log' ) ); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Saves the given Twitter username and user ID to the API. |
| 53 |
* |
| 54 |
* @since 4.1.0 |
| 55 |
*/ |
| 56 |
public function username_save_twitter() { |
| 57 |
|
| 58 |
// Run a security check first. |
| 59 |
check_ajax_referer( $this->base->plugin->name . '-username-save-twitter', 'nonce' ); |
| 60 |
|
| 61 |
// Bail if no user ID or username was provided. |
| 62 |
if ( ! isset( $_REQUEST['user_id'] ) ) { |
| 63 |
wp_send_json_error( __( 'No user ID was provided.', 'wp-to-buffer' ) ); |
| 64 |
} |
| 65 |
if ( ! isset( $_REQUEST['username'] ) ) { |
| 66 |
wp_send_json_error( __( 'No username was provided.', 'wp-to-buffer' ) ); |
| 67 |
} |
| 68 |
|
| 69 |
// Sanitize inputs. |
| 70 |
$user_id = sanitize_text_field( wp_unslash( $_REQUEST['user_id'] ) ); |
| 71 |
$username = sanitize_text_field( wp_unslash( $_REQUEST['username'] ) ); |
| 72 |
|
| 73 |
// Save. |
| 74 |
$results = $this->base->get_class( 'twitter_api' )->username_save( $user_id, $username ); |
| 75 |
|
| 76 |
wp_send_json_success( $results ); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Saves statuses for the given Post Type in the Plugin's Settings section. |
| 82 |
* |
| 83 |
* @since 4.0.8 |
| 84 |
*/ |
| 85 |
public function save_statuses() { |
| 86 |
|
| 87 |
// Run a security check first. |
| 88 |
check_ajax_referer( $this->base->plugin->name . '-save-statuses', 'nonce' ); |
| 89 |
|
| 90 |
// Bail if no post type was provided. |
| 91 |
if ( ! isset( $_REQUEST['post_type'] ) ) { |
| 92 |
wp_send_json_error( __( 'No post type was provided.', 'wp-to-buffer' ) ); |
| 93 |
} |
| 94 |
if ( ! isset( $_REQUEST['statuses'] ) ) { |
| 95 |
wp_send_json_error( __( 'No statuses were provided.', 'wp-to-buffer' ) ); |
| 96 |
} |
| 97 |
|
| 98 |
// Parse request. |
| 99 |
$post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); |
| 100 |
$statuses = json_decode( wp_unslash( $_REQUEST['statuses'] ), true ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 101 |
|
| 102 |
// Get some other information. |
| 103 |
$post_type_object = get_post_type_object( $post_type ); |
| 104 |
$documentation_url = $this->base->plugin->documentation_url . '/status-settings'; |
| 105 |
|
| 106 |
// Save and return. |
| 107 |
$result = $this->base->get_class( 'settings' )->update_settings( $post_type, $statuses ); |
| 108 |
|
| 109 |
// Bail if an error occured. |
| 110 |
if ( is_wp_error( $result ) ) { |
| 111 |
wp_send_json_error( $result->get_error_message() ); |
| 112 |
} |
| 113 |
|
| 114 |
// Return success, with flag denoting if the Post Type is configured to send statuses. |
| 115 |
wp_send_json_success( |
| 116 |
array( |
| 117 |
'post_type_enabled' => $this->base->get_class( 'settings' )->is_post_type_enabled( $post_type ), |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns HTML markup that can be injected inside a <tr> to show the status' information |
| 125 |
* |
| 126 |
* @since 4.4.0 |
| 127 |
*/ |
| 128 |
public function get_status_row() { |
| 129 |
|
| 130 |
// Run a security check first. |
| 131 |
check_ajax_referer( $this->base->plugin->name . '-get-status-row', 'nonce' ); |
| 132 |
|
| 133 |
// Bail if expect parameters were not was provided. |
| 134 |
if ( ! isset( $_REQUEST['status'] ) ) { |
| 135 |
wp_send_json_error( __( 'No status was provided.', 'wp-to-buffer' ) ); |
| 136 |
} |
| 137 |
if ( ! isset( $_REQUEST['post_type'] ) ) { |
| 138 |
wp_send_json_error( __( 'No post type was provided.', 'wp-to-buffer' ) ); |
| 139 |
} |
| 140 |
if ( ! isset( $_REQUEST['post_action'] ) ) { |
| 141 |
wp_send_json_error( __( 'No post action was provided.', 'wp-to-buffer' ) ); |
| 142 |
} |
| 143 |
|
| 144 |
// Parse request. |
| 145 |
$status = json_decode( wp_unslash( $_REQUEST['status'] ), true ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 146 |
$post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); |
| 147 |
$action = sanitize_text_field( wp_unslash( $_REQUEST['post_action'] ) ); |
| 148 |
|
| 149 |
// Return array of row data (message, image, schedule). |
| 150 |
wp_send_json_success( $this->base->get_class( 'settings' )->get_status_row( $status, $post_type, $action ) ); |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Fetches the plugin log for the given Post ID, in HTML format |
| 156 |
* compatible for insertion into the Log Table. |
| 157 |
* |
| 158 |
* @since 3.0.0 |
| 159 |
*/ |
| 160 |
public function get_log() { |
| 161 |
|
| 162 |
// Run a security check first. |
| 163 |
check_ajax_referer( $this->base->plugin->name . '-get-log', 'nonce' ); |
| 164 |
|
| 165 |
// Bail if no post ID was provided. |
| 166 |
if ( ! isset( $_REQUEST['post'] ) ) { |
| 167 |
wp_send_json_error( __( 'No post ID was provided.', 'wp-to-buffer' ) ); |
| 168 |
} |
| 169 |
|
| 170 |
// Get Post ID. |
| 171 |
$post_id = absint( $_REQUEST['post'] ); |
| 172 |
|
| 173 |
// Return log table output. |
| 174 |
wp_send_json_success( $this->base->get_class( 'log' )->build_log_table_output( $this->base->get_class( 'log' )->get( $post_id ) ) ); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Clears the plugin log for the given Post ID |
| 180 |
* |
| 181 |
* @since 3.0.0 |
| 182 |
*/ |
| 183 |
public function clear_log() { |
| 184 |
|
| 185 |
// Run a security check first. |
| 186 |
check_ajax_referer( $this->base->plugin->name . '-clear-log', 'nonce' ); |
| 187 |
|
| 188 |
// Bail if no post ID was provided. |
| 189 |
if ( ! isset( $_REQUEST['post'] ) ) { |
| 190 |
wp_send_json_error( __( 'No post ID was provided.', 'wp-to-buffer' ) ); |
| 191 |
} |
| 192 |
|
| 193 |
// Get Post ID. |
| 194 |
$post_id = absint( $_REQUEST['post'] ); |
| 195 |
|
| 196 |
// Clear log. |
| 197 |
$this->base->get_class( 'log' )->delete_by_post_id( $post_id ); |
| 198 |
|
| 199 |
wp_send_json_success(); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
|