Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 14  | 
               | 
       0.00%  | 
       0 / 2  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| AJAX |         | 
       0.00%  | 
       0 / 14  | 
               | 
       0.00%  | 
       0 / 2  | 
       42 |         | 
       0.00%  | 
       0 / 1  | 
      
| __construct |         | 
       0.00%  | 
       0 / 2  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| set_plugin_name |         | 
       0.00%  | 
       0 / 12  | 
               | 
       0.00%  | 
       0 / 1  | 
       30 | |||
| 1 | <?php | 
| 2 | /** | 
| 3 | * AJAX handler for edits to plugins.php | 
| 4 | * | 
| 5 | * @package brianhenryie/bh-wp-plugins-page | 
| 6 | */ | 
| 7 | |
| 8 | namespace BrianHenryIE\WP_Plugins_Page\Admin; | 
| 9 | |
| 10 | use BrianHenryIE\WP_Plugins_Page\API\API; | 
| 11 | use Psr\Log\LoggerAwareTrait; | 
| 12 | use Psr\Log\LoggerInterface; | 
| 13 | |
| 14 | /** | 
| 15 | * Handle the wp-ajax request. | 
| 16 | */ | 
| 17 | class AJAX { | 
| 18 | use LoggerAwareTrait; | 
| 19 | |
| 20 | /** | 
| 21 | * The plugin functions. | 
| 22 | */ | 
| 23 | protected API $api; | 
| 24 | |
| 25 | /** | 
| 26 | * Constructor | 
| 27 | * | 
| 28 | * @param API $api The plugin functions. | 
| 29 | * @param LoggerInterface $logger A PSR logger. | 
| 30 | */ | 
| 31 | public function __construct( API $api, LoggerInterface $logger ) { | 
| 32 | $this->setLogger( $logger ); | 
| 33 | $this->api = $api; | 
| 34 | } | 
| 35 | |
| 36 | /** | 
| 37 | * Validates the AJAX request, use API to act, returns JSON response. | 
| 38 | * | 
| 39 | * @see API::set_plugin_name() | 
| 40 | * | 
| 41 | * @hooked wp_ajax_bh_wp_plugins_page_set_plugin_name | 
| 42 | */ | 
| 43 | public function set_plugin_name(): void { | 
| 44 | |
| 45 | if ( ! check_ajax_referer( self::class, false, false ) ) { | 
| 46 | wp_send_json_error( array( 'message' => 'Bad/no nonce.' ), 401 ); | 
| 47 | } | 
| 48 | |
| 49 | if ( ! current_user_can( 'manage_options' ) ) { | 
| 50 | wp_send_json_error( array( 'message' => 'Unauthorized' ), 401 ); | 
| 51 | } | 
| 52 | |
| 53 | if ( ! isset( $_POST['pluginBasename'], $_POST['pluginName'] ) ) { | 
| 54 | wp_send_json_error( array( 'message' => 'Bad request.' ), 400 ); | 
| 55 | } | 
| 56 | |
| 57 | $plugin_basename = sanitize_text_field( wp_unslash( $_POST['pluginBasename'] ) ); | 
| 58 | $plugin_name = sanitize_text_field( wp_unslash( $_POST['pluginName'] ) ); | 
| 59 | |
| 60 | try { | 
| 61 | $result = $this->api->set_plugin_name( $plugin_basename, $plugin_name ); | 
| 62 | } catch ( \Exception $exception ) { | 
| 63 | wp_send_json_error( array( 'message' => $exception->getMessage() ), 500 ); | 
| 64 | } | 
| 65 | |
| 66 | wp_send_json_success( $result ); | 
| 67 | } | 
| 68 | |
| 69 | } |