Skip to content

Commit 6ab53a6

Browse files
xxsimoxxxxsimoxx
andauthored
Show results from page 1 when performing a new search (#34)
* Add more meaningful informations to errors * Fix pagination in searches * Change input type to search * Bump version to 1.1.3 --------- Co-authored-by: xxsimoxx <simone@gieffeedizioni.it>
1 parent 61041ee commit 6ab53a6

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

classes/PluginInstall.class.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,16 @@ public static function do_directory_request($args = [], $type = 'plugins')
161161

162162
$e = wp_remote_retrieve_response_code($response);
163163
if ($e !== 200) {
164-
$result['error'] = $response['response']['message'];
164+
$result['error'] = $response['response']['message'].'.';
165165
$result['code'] = $response['response']['code'];
166+
if (!isset($response['body']) || !json_validate($response['body'])) {
167+
return $result;
168+
}
169+
$api_message = json_decode($response['body'], true);
170+
if(!isset($api_message['message'])) {
171+
return $result;
172+
}
173+
$result['error'] .= ' '.$api_message['message'];
166174
return $result;
167175
}
168176

@@ -343,6 +351,11 @@ public function render_menu()
343351
if (isset($_REQUEST['searchfor'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
344352
$args['search'] = sanitize_text_field(wp_unslash($_REQUEST['searchfor'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended
345353
}
354+
// Reset paginations for new searches
355+
$searching = $args['search'] ?? '';
356+
if (isset($_REQUEST['searchingfor']) && $_REQUEST['searchingfor'] !== $searching) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
357+
$args['page'] = 1;
358+
}
346359

347360
$result = $this->do_directory_request($args);
348361
if ($result['success'] === false) {
@@ -371,10 +384,11 @@ public function render_menu()
371384
<form method="GET" action="<?php echo esc_url(add_query_arg(['page' => 'classicpress-directory-integration-plugin-install'], remove_query_arg(['getpage']))); ?>">
372385
<p class="cp-plugin-search-box">
373386
<label for="searchfor" class="screen-reader-text" ><?php echo esc_html__('Search for plugins', 'classicpress-directory-integration'); ?></label><br>
374-
<input type="text" id="searchfor" name="searchfor" class="wp-filter-search" placeholder="<?php echo esc_html__('Search for a plugin...', 'classicpress-directory-integration'); ?>"><br>
387+
<input type="hidden" name="searchingfor" value="<?php echo esc_html($searching); ?>">
388+
<input type="search" id="searchfor" name="searchfor" class="wp-filter-search" <?php echo $searching !== '' ? 'value="'.esc_html($searching).'" ' : '' ?>placeholder="<?php echo esc_html__('Search for a plugin...', 'classicpress-directory-integration'); ?>"><br>
375389
<?php
376390
foreach ((array) $_GET as $key => $val) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
377-
if (in_array($key, ['searchfor'])) {
391+
if (in_array($key, ['searchfor', 'getpage'])) {
378392
continue;
379393
}
380394
echo '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_html($val) . '" />';

classes/ThemeInstall.class.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,16 @@ public static function do_directory_request($args = [], $type = 'themes')
155155

156156
$e = wp_remote_retrieve_response_code($response);
157157
if ($e !== 200) {
158-
$result['error'] = $response['response']['message'];
158+
$result['error'] = $response['response']['message'].'.';
159159
$result['code'] = $response['response']['code'];
160+
if (!isset($response['body']) || !json_validate($response['body'])) {
161+
return $result;
162+
}
163+
$api_message = json_decode($response['body'], true);
164+
if(!isset($api_message['message'])) {
165+
return $result;
166+
}
167+
$result['error'] .= ' '.$api_message['message'];
160168
return $result;
161169
}
162170

@@ -340,6 +348,11 @@ public function render_menu()
340348
if (isset($_REQUEST['searchfor'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
341349
$args['search'] = sanitize_text_field(wp_unslash($_REQUEST['searchfor'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended
342350
}
351+
// Reset paginations for new searches
352+
$searching = $args['search'] ?? '';
353+
if (isset($_REQUEST['searchingfor']) && $_REQUEST['searchingfor'] !== $searching) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
354+
$args['page'] = 1;
355+
}
343356

344357
$result = $this->do_directory_request($args);
345358
if ($result['success'] === false) {
@@ -368,10 +381,11 @@ public function render_menu()
368381
<form method="GET" action="<?php echo esc_url(add_query_arg(['page' => 'classicpress-directory-integration-theme-install'], remove_query_arg(['getpage']))); ?>">
369382
<p class="cp-plugin-search-box">
370383
<label for="searchfor" class="screen-reader-text"><?php echo esc_html__('Search for a theme', 'classicpress-directory-integration'); ?></label><br>
371-
<input type="text" id="searchfor" name="searchfor" class="wp-filter-search" placeholder="<?php echo esc_html__('Search for a theme...', 'classicpress-directory-integration'); ?>"><br>
384+
<input type="hidden" name="searchingfor" value="<?php echo esc_html($searching); ?>">
385+
<input type="search" id="searchfor" name="searchfor" class="wp-filter-search" <?php echo $searching !== '' ? 'value="'.esc_html($searching).'" ' : '' ?>placeholder="<?php echo esc_html__('Search for a theme...', 'classicpress-directory-integration'); ?>"><br>
372386
<?php
373387
foreach ((array) $_GET as $key => $val) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
374-
if (in_array($key, ['searchfor'])) {
388+
if (in_array($key, ['searchfor', 'getpage'])) {
375389
continue;
376390
}
377391
echo '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_html($val) . '" />';

classicpress-directory-integration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* -----------------------------------------------------------------------------
55
* Plugin Name: ClassicPress Directory Integration
66
* Description: Install and update plugins and themes from ClassicPress directory.
7-
* Version: 1.1.2
7+
* Version: 1.1.3
88
* Author: ClassicPress Contributors
99
* Author URI: https://www.classicpress.net
1010
* Plugin URI: https://www.classicpress.net

0 commit comments

Comments
 (0)