Skip to content

Commit

Permalink
Improved error display when searching for feeds. Working toward #49
Browse files Browse the repository at this point in the history
  • Loading branch information
jackjamieson2 committed May 30, 2019
1 parent 79009e6 commit 88dbc58
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 47 deletions.
84 changes: 58 additions & 26 deletions includes/class-yarns-microsub-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
class Yarns_Microsub_Admin {



/**
* Stores the page query var for Yarns' options page.
*
Expand All @@ -27,13 +26,14 @@ public static function admin_page_link( $args = [] ) {
//return add_query_arg( array( 'page' => static::$options_page_name ), admin_url() . 'admin.php' );
}

public static function admin_channel_settings_link($uid, $args = []) {
public static function admin_channel_settings_link( $uid, $args = [] ) {
$default_args = array(
'mode' => 'channel-settings',
'channel' => $uid,
);
// merge additional args if defined.
$args = array_merge( $default_args, $args );

return static::admin_page_link( $args );
}

Expand All @@ -44,11 +44,11 @@ public static function admin_channel_feeds_link( $uid, $args = [] ) {
);
// merge additional args if defined.
$args = array_merge( $default_args, $args );

return static::admin_page_link( $args );
}



/**
* Initialize the admin screen by adding actions for ajax calls.
*/
Expand Down Expand Up @@ -178,7 +178,7 @@ public static function save_options() {
}

if ( isset( $options['show_debug'] ) ) {
$show_debug = $options['show_debug'] === 'true'? true: false;
$show_debug = $options['show_debug'] === 'true' ? true : false;
update_option( 'yarns_show_debug', $show_debug );
$results .= 'updated show_debug. ';
}
Expand All @@ -189,7 +189,6 @@ public static function save_options() {
}



/**
* Returns HTML with form for adding new channels
*
Expand Down Expand Up @@ -235,9 +234,6 @@ public static function list_channels() {
}





/**
* Echoes HTML for the debug log
*/
Expand Down Expand Up @@ -267,6 +263,7 @@ private static function debug_commands() {
$html = '<h2> Debug commands </h2>';
$html .= '<a class="button" id="yarns_force_poll">Force poll</a><br><br>';
$html .= '<a class="button" id="yarns_delete_posts">Delete all posts</a>';

return $html;
}

Expand All @@ -277,23 +274,57 @@ public static function find_feeds() {
if ( isset( $_POST['query'] ) ) {
$query = sanitize_text_field( wp_unslash( $_POST['query'] ) );

$results = Yarns_Microsub_Parser::search( $query )['results'];
$response = Yarns_Microsub_Parser::search( $query );
$response = static::validate_results( $response, 'search' );

if ( empty( $results ) ) {
echo 'No feeds found';
if ( $response['error'] ) {
echo wp_json_encode( $response );
wp_die();
} else {
$html = '<h3>Select a feed to follow:</h3>';
foreach ( $response['content']['results'] as $result ) {
$html .= '<label><input type="radio" name="yarns-feed-picker" value="' . $result['url'] . '">' . $result['url'] . '</label>';
}
$html .= '<a class="button" id ="yarns-channel-preview-feed">Preview</a>';
$html .= '<a class="button" id ="yarns-channel-add-feed">Subscribe</a>';

echo wp_json_encode(
array(
'error' => false,
'content' => $html,
)
);
wp_die();
}
}
}

$html = '<h3>Select a feed to follow:</h3>';
foreach ( $results as $result ) {
$html .= '<label><input type="radio" name="yarns-feed-picker" value="' . $result['url'] . '">' . $result['url'] . '</label>';
private static function validate_results( $response, $action ) {
// General error reporting.
if ( is_wp_error( $response ) ) {
return array(
'error' => true,
'content' => $response->get_error_message(),
);
}

}
$html .= '<a class="button" id ="yarns-channel-preview-feed">Preview</a>';
$html .= '<a class="button" id ="yarns-channel-add-feed">Subscribe</a>';
echo $html;
// Additional errors for specific actions.
switch ( $action ) {
case 'search':
// Check if the results are empty.
if ( empty( $response['results'] ) ) {
return array(
'error' => true,
'content' => 'No feeds were found.',
);
}
}
wp_die();

// If there were no errors, return the validated results.
return array (
'error' => false,
'content' => $response,
);
}

/**
Expand All @@ -309,7 +340,7 @@ public static function follow_feed() {
$url = sanitize_text_field( wp_unslash( $_POST['url'] ) );
Yarns_Microsub_Channels::follow( $uid, $url );
//$channel = Yarns_Microsub_Channels::get_channel( $uid );
echo static::admin_channel_feeds_link($uid);
echo static::admin_channel_feeds_link( $uid );
//echo static::yarns_list_feeds( $channel );
}

Expand All @@ -327,7 +358,7 @@ public static function unfollow_feed() {
$url = sanitize_text_field( wp_unslash( $_POST['url'] ) );
Yarns_Microsub_Channels::follow( $uid, $url, $unfollow = true );
//$channel = Yarns_Microsub_Channels::get_channel( $uid );
echo static::admin_channel_feeds_link($uid);
echo static::admin_channel_feeds_link( $uid );

//echo static::yarns_list_feeds( $channel );
}
Expand Down Expand Up @@ -380,19 +411,19 @@ public static function delete_channel() {
/**
* Echoes a preview of a feed.
*/
public static function preview_feed($url = null) {
if ( ! $url) {
public static function preview_feed( $url = null ) {
if ( ! $url ) {
if ( isset( $_POST['url'] ) ) {
$url = sanitize_text_field( wp_unslash( $_POST['url'] ) );
}
}


$preview_data = Yarns_Microsub_Parser::preview($url);
$preview_data = Yarns_Microsub_Parser::preview( $url );
//echo wp_json_encode($preview_data);
//wp_die();

$preview = new Yarns_Microsub_Preview($preview_data);
$preview = new Yarns_Microsub_Preview( $preview_data );
$preview_html = $preview->html();
echo $preview_html;

Expand All @@ -401,4 +432,5 @@ public static function preview_feed($url = null) {



}

}
64 changes: 43 additions & 21 deletions js/yarns_microsub_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,34 +237,56 @@
* Manage subscriptions
*/


/**
* Search for feeds
*
*/
$( 'body' ).on( 'click', '#yarns-channel-find-feeds', function () {
query = $( '#yarns-URL-input' ).val()
console.log( 'Searching for ' + query )
button = $( this )
start_loading( button )
$( 'body' ).on(
'click',
'#yarns-channel-find-feeds',
function () {
clear_errors() // Clear any errors that are showing from a previous function.
let errors = false
button = $( this )
query = $( '#yarns-URL-input' ).val()

$.ajax( {
url: yarns_microsub_server_ajax.ajax_url,
type: 'post',
data: {
action: 'find_feeds',
query: query,
},
success: function ( response ) {
done_loading( button )
console.log( 'success' )
$( '#yarns-feed-picker-list' ).html( response )
// Validate query.
if ( query.length < 1 ) {
display_error( button, 'Please enter a query before searching.' )
errors = true
}

// If there were no errors, perform the search.
if ( errors == false ) {
console.log( 'Searching for ' + query )

$( '#yarns-feed-picker-list input' ).first().trigger( 'click' )
start_loading( button )
$.ajax(
{
url: yarns_microsub_server_ajax.ajax_url,
type: 'post',
data: {
action: 'find_feeds',
query: query,
},
success: function ( response ) {
response = JSON.parse( response )
done_loading( button )
console.log( response )
if (response['error']) {
display_error( button, response['content'] )
} else {
$( '#yarns-feed-picker-list' ).html( response['content'] )

}
} )
$( '#yarns-feed-picker-list input' ).first().trigger( 'click' )
}

} )
}
}
)
}
}
)

/**
* Display a preview
Expand Down

0 comments on commit 88dbc58

Please sign in to comment.