id, $this->wizard_slug ) !== false ) {
return;
}
?>
wizard_slug,
array( $this, 'render_wizard_page' )
);
}
/**
* Render the wizard page.
*
* @return void
*/
public function render_wizard_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'plugetune' ) );
}
if ( ! wp_style_is( 'font-awesome', 'enqueued' ) && ! wp_style_is( 'font-awesome', 'registered' ) ) {
wp_enqueue_style(
'font-awesome',
PLUGETUNE_URL . 'assets/vendor/font-awesome/css/all.min.css',
array(),
'6.4.0'
);
}
$api_key = get_option( 'plugetune_api_key', '' );
$channel_ids = get_option( 'plugetune_allowed_channel_ids', '' );
?>
'Security check failed.' ) );
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => 'Unauthorized access.' ) );
return;
}
$api_key = isset( $_POST['api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) : '';
$channel_ids = isset( $_POST['channel_ids'] ) ? sanitize_text_field( wp_unslash( $_POST['channel_ids'] ) ) : '';
if ( empty( $api_key ) ) {
wp_send_json_error( array( 'message' => 'Please enter your YouTube API Key.' ) );
return;
}
if ( empty( $channel_ids ) ) {
wp_send_json_error( array( 'message' => 'Please enter at least one YouTube Channel ID.' ) );
return;
}
$channel_ids_array = array_map( 'trim', explode( ',', $channel_ids ) );
$channel_ids_array = array_filter( $channel_ids_array );
if ( empty( $channel_ids_array ) ) {
wp_send_json_error( array( 'message' => 'No valid Channel IDs provided.' ) );
return;
}
$invalid_channels = array();
$valid_channels = array();
foreach ( $channel_ids_array as $channel_id ) {
$channel_url = "https://www.googleapis.com/youtube/v3/channels?part=snippet&id={$channel_id}&key={$api_key}";
$channel_response = wp_remote_get(
$channel_url,
array(
'timeout' => 30,
'headers' => array(
'Accept' => 'application/json',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
),
'sslverify' => true,
)
);
if ( is_wp_error( $channel_response ) ) {
$invalid_channels[] = $channel_id . ' (connection failed)';
continue;
}
$channel_data = json_decode( wp_remote_retrieve_body( $channel_response ) );
if ( isset( $channel_data->error ) ) {
$invalid_channels[] = $channel_id . ' (' . ( isset( $channel_data->error->message ) ? $channel_data->error->message : 'API error' ) . ')';
continue;
}
if ( empty( $channel_data->items ) ) {
$invalid_channels[] = $channel_id;
continue;
}
$valid_channels[] = $channel_id;
}
if ( ! empty( $invalid_channels ) ) {
wp_send_json_error(
array(
'message' => 'The following Channel ID(s) could not be verified:
' . implode( '
', array_map( 'esc_html', $invalid_channels ) ) . '
Please check that each Channel ID is correct (starts with "UC" and is 24 characters long) and belongs to a valid YouTube channel.',
)
);
return;
}
update_option( 'plugetune_api_key', $api_key );
update_option( 'plugetune_allowed_channel_ids', implode( ',', $valid_channels ) );
wp_send_json_success( array( 'message' => 'Settings saved successfully! ' . count( $valid_channels ) . ' channel(s) verified.' ) );
}
/**
* AJAX: Verify the provided API key against the YouTube Data API.
*
* @return void
*/
public function ajax_check_api() {
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'plugetune_ajax_nonce' ) ) {
wp_send_json_error( array( 'message' => 'Security check failed.' ) );
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => 'Unauthorized access.' ) );
return;
}
$api_key = isset( $_POST['api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) : '';
if ( empty( $api_key ) ) {
wp_send_json_error( array( 'message' => 'Please enter your YouTube API Key.' ) );
return;
}
$test_url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=dQw4w9WgXcQ&key={$api_key}";
$response = wp_remote_get(
$test_url,
array(
'timeout' => 45,
'headers' => array(
'Accept' => 'application/json',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
),
'sslverify' => true,
)
);
if ( is_wp_error( $response ) ) {
wp_send_json_error( array( 'message' => 'Connection failed: ' . $response->get_error_message() ) );
return;
}
$data = json_decode( wp_remote_retrieve_body( $response ) );
if ( isset( $data->error ) ) {
$error_message = '';
if ( isset( $data->error->errors[0]->reason ) ) {
switch ( $data->error->errors[0]->reason ) {
case 'keyInvalid':
$error_message = 'Your API key is invalid. Please check your key and try again.';
break;
case 'keyExpired':
$error_message = 'Your API key has expired. Please generate a new key.';
break;
case 'accessNotConfigured':
$error_message = 'YouTube Data API v3 is not enabled for this project. Please enable it in Google Cloud Console.';
break;
case 'quotaExceeded':
$error_message = 'API quota exceeded. Please try again later or upgrade your quota.';
break;
default:
$error_message = isset( $data->error->errors[0]->message ) ? $data->error->errors[0]->message : 'Invalid API key.';
}
} else {
$error_message = isset( $data->error->message ) ? $data->error->message : 'Invalid API key.';
}
wp_send_json_error( array( 'message' => $error_message ) );
return;
}
wp_send_json_success( array( 'message' => 'API key is valid and working correctly!' ) );
}
/**
* AJAX: Mark the wizard as skipped.
*
* @return void
*/
public function ajax_skip_wizard() {
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'plugetune_ajax_nonce' ) ) {
wp_send_json_error();
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error();
return;
}
update_option( 'plugetune_wizard_skipped', true );
wp_send_json_success();
}
/**
* Enqueue the wizard assets.
*
* @param string $hook Current admin page hook.
* @return void
*/
public function enqueue_assets( $hook ) {
if ( strpos( $hook, $this->wizard_slug ) === false ) {
return;
}
wp_enqueue_script( 'jquery' );
wp_enqueue_style(
'font-awesome',
PLUGETUNE_URL . 'assets/vendor/font-awesome/css/all.min.css',
array(),
'6.4.0'
);
wp_enqueue_style(
'plugetune-setup-wizard',
PLUGETUNE_URL . 'assets/css/plugetune_setup-wizard.css',
array(),
PLUGETUNE_VERSION
);
wp_enqueue_script(
'plugetune-wizard',
PLUGETUNE_URL . 'assets/js/plugetune_setup-wizard.js',
array( 'jquery' ),
PLUGETUNE_VERSION,
true
);
wp_localize_script(
'plugetune-wizard',
'plugetune_ajax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'plugetune_ajax_nonce' ),
)
);
wp_localize_script(
'plugetune-wizard',
'plugetune_wizard_config',
array(
'redirect_url' => admin_url( 'edit.php?post_type=plugetune_video' ),
)
);
}
}
$plugetune_wizard = PlugeTune_Setup_Wizard::get_instance();
$plugetune_wizard->init();