Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
14.29% |
4 / 28 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| API | |
14.29% |
4 / 28 |
|
50.00% |
1 / 2 |
28.67 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set_plugin_name | |
11.11% |
3 / 27 |
|
0.00% |
0 / 1 |
22.56 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Function to save the updated plugin title. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-plugins-page |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Plugins_Page\API; |
| 9 | |
| 10 | use Exception; |
| 11 | use Psr\Log\LoggerAwareTrait; |
| 12 | use Psr\Log\LoggerInterface; |
| 13 | |
| 14 | /** |
| 15 | * Saves changes in wp_options which are merged with WordPress's `get_plugins()`. |
| 16 | */ |
| 17 | class API { |
| 18 | use LoggerAwareTrait; |
| 19 | |
| 20 | const PLUGINS_PAGE_CHANGES_OPTION_NAME = 'bh_wp_plugins_page_changes'; |
| 21 | const PLUGINS_PAGE_UPDATES_DATES_OPTION_NAME = 'bh_wp_plugins_page_plugin_update_available_dates'; |
| 22 | const PLUGINS_PAGE_INSTALL_DATES_OPTION_NAME = 'bh_wp_plugins_page_plugin_install_dates'; |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | * |
| 27 | * @param LoggerInterface $logger A PSR logger. |
| 28 | */ |
| 29 | public function __construct( LoggerInterface $logger ) { |
| 30 | $this->setLogger( $logger ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Save the desired plugin title. |
| 35 | * |
| 36 | * Also save the original title. |
| 37 | * |
| 38 | * @param string $plugin_basename The plugin whose title is being updated. |
| 39 | * @param string $new_name The name to set, or empty to reset to the original. |
| 40 | * |
| 41 | * @return array{updated:bool,plugin_basename:string,before:array,after:array,Name:string} |
| 42 | * @throws Exception When the plugin whose title is being updated is not found in WordPress's plugin list. |
| 43 | */ |
| 44 | public function set_plugin_name( string $plugin_basename, string $new_name ): array { |
| 45 | |
| 46 | $plugins = get_plugins(); |
| 47 | |
| 48 | if ( ! isset( $plugins[ $plugin_basename ] ) ) { |
| 49 | throw new Exception( "Plugin {$plugin_basename} not found in WordPress plugins array." ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * The existing saved changes. |
| 54 | * |
| 55 | * @var array<string, array{Name:string,Original_Name:string}> $saved_changes |
| 56 | */ |
| 57 | $saved_changes = get_option( self::PLUGINS_PAGE_CHANGES_OPTION_NAME, array() ); |
| 58 | |
| 59 | if ( ! isset( $saved_changes[ $plugin_basename ] ) ) { |
| 60 | $saved_changes[ $plugin_basename ] = array(); |
| 61 | } |
| 62 | |
| 63 | $before = $saved_changes[ $plugin_basename ]; |
| 64 | |
| 65 | if ( empty( $new_name ) ) { |
| 66 | |
| 67 | unset( $saved_changes[ $plugin_basename ]['Name'] ); |
| 68 | unset( $saved_changes[ $plugin_basename ]['Original_Name'] ); |
| 69 | if ( empty( $saved_changes[ $plugin_basename ] ) ) { |
| 70 | unset( $saved_changes[ $plugin_basename ] ); |
| 71 | $after = null; |
| 72 | } else { |
| 73 | $after = $saved_changes[ $plugin_basename ]; |
| 74 | } |
| 75 | |
| 76 | $name = $plugins[ $plugin_basename ]['Name']; |
| 77 | } else { |
| 78 | |
| 79 | $saved_changes[ $plugin_basename ]['Name'] = $new_name; |
| 80 | $saved_changes[ $plugin_basename ]['Original_Name'] = $plugins[ $plugin_basename ]['Name']; |
| 81 | |
| 82 | $after = $saved_changes[ $plugin_basename ]; |
| 83 | |
| 84 | $name = $new_name; |
| 85 | } |
| 86 | |
| 87 | $updated = update_option( self::PLUGINS_PAGE_CHANGES_OPTION_NAME, $saved_changes ); |
| 88 | |
| 89 | return array( |
| 90 | 'updated' => $updated, |
| 91 | 'plugin_basename' => $plugin_basename, |
| 92 | 'before' => $before, |
| 93 | 'after' => $after, |
| 94 | 'Name' => $name, |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | } |