Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.76% |
50 / 66 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Actions | |
75.76% |
50 / 66 |
|
71.43% |
5 / 7 |
11.42 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| add_cron_hooks | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| add_wordpress_updater_hooks | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
| add_plugins_page_modal_hooks | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
| add_assets_hooks | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| add_rest_hooks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| add_cli_hooks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WordPress hooks and filters for licence management. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-plugin-updater |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Plugin_Updater; |
| 9 | |
| 10 | use BrianHenryIE\WP_Plugin_Updater\Admin\Admin_Assets; |
| 11 | use BrianHenryIE\WP_Plugin_Updater\Admin\Licence_Management_Tab; |
| 12 | use BrianHenryIE\WP_Plugin_Updater\Admin\Plugins_Page; |
| 13 | use BrianHenryIE\WP_Plugin_Updater\Admin\Plugins_Page_View_Details; |
| 14 | use BrianHenryIE\WP_Plugin_Updater\WP_Includes\CLI; |
| 15 | use BrianHenryIE\WP_Plugin_Updater\WP_Includes\Cron; |
| 16 | use BrianHenryIE\WP_Plugin_Updater\WP_Includes\Rest; |
| 17 | use BrianHenryIE\WP_Plugin_Updater\WP_Includes\WordPress_Updater; |
| 18 | use Psr\Log\LoggerInterface; |
| 19 | |
| 20 | /** |
| 21 | * `add_action` and `add_filter` hooks. |
| 22 | */ |
| 23 | class Actions { |
| 24 | /** |
| 25 | * Constructor. |
| 26 | * |
| 27 | * @param API_Interface $api The updater's main functions. |
| 28 | * @param Settings_Interface $settings The configuration for the updater component. |
| 29 | * @param LoggerInterface $logger A PSR logger. |
| 30 | */ |
| 31 | public function __construct( |
| 32 | protected API_Interface $api, |
| 33 | protected Settings_Interface $settings, |
| 34 | protected LoggerInterface $logger, |
| 35 | ) { |
| 36 | $this->add_plugins_page_modal_hooks(); |
| 37 | $this->add_rest_hooks(); |
| 38 | $this->add_assets_hooks(); |
| 39 | $this->add_cron_hooks(); |
| 40 | $this->add_wordpress_updater_hooks(); |
| 41 | $this->add_cli_hooks(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Add hooks to schedule and handle a daily update check. |
| 46 | */ |
| 47 | protected function add_cron_hooks(): void { |
| 48 | $cron = new Cron( |
| 49 | $this->api, |
| 50 | $this->settings |
| 51 | ); |
| 52 | |
| 53 | // Is this enough? If it's deleted once, it will be deleted forever. |
| 54 | add_action( |
| 55 | "activate_{$this->settings->get_plugin_slug()}", |
| 56 | array( $cron, 'register_cron_job' ) |
| 57 | ); |
| 58 | |
| 59 | add_action( |
| 60 | $cron->get_update_check_cron_job_name(), |
| 61 | array( $cron, 'handle_update_check_cron_job' ) |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Add hooks to add the product and licence information to the WordPress `get_plugins()` information array. |
| 67 | */ |
| 68 | protected function add_wordpress_updater_hooks(): void { |
| 69 | |
| 70 | $hostname = wp_parse_url( sanitize_url( $this->settings->get_licence_server_host() ), PHP_URL_HOST ); |
| 71 | |
| 72 | $plugin_update = new WordPress_Updater( |
| 73 | $this->api, |
| 74 | $this->settings, |
| 75 | $this->logger, |
| 76 | ); |
| 77 | |
| 78 | add_filter( |
| 79 | 'pre_set_site_transient_update_plugins', |
| 80 | array( $plugin_update, 'detect_force_update' ), |
| 81 | 10, |
| 82 | 2 |
| 83 | ); |
| 84 | |
| 85 | add_filter( |
| 86 | "update_plugins_{$hostname}", |
| 87 | array( $plugin_update, 'add_update_information' ), |
| 88 | 10, |
| 89 | 4 |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Add hooks to add the "View details" modal to `plugins.php`. |
| 95 | * |
| 96 | * TODO: check the use of `plugins_api` vs `plugins_api_result` filters, which is correct? |
| 97 | */ |
| 98 | protected function add_plugins_page_modal_hooks(): void { |
| 99 | |
| 100 | $view_details = new Plugins_Page_View_Details( $this->api, $this->settings ); |
| 101 | add_filter( 'plugins_api', array( $view_details, 'add_plugin_modal_data' ), 10, 3 ); |
| 102 | |
| 103 | $licence_tab = new Licence_Management_Tab( $this->api, $this->settings ); |
| 104 | add_filter( 'plugins_api_result', array( $licence_tab, 'add_licence_tab' ), 10, 3 ); |
| 105 | |
| 106 | $plugins_page = new Plugins_Page( |
| 107 | $this->api, |
| 108 | $this->settings |
| 109 | ); |
| 110 | add_filter( 'plugin_auto_update_setting_html', array( $plugins_page, 'plugin_auto_update_setting_html' ), 10, 2 ); |
| 111 | |
| 112 | add_action( |
| 113 | "in_plugin_update_message-{$this->settings->get_plugin_basename()}", |
| 114 | array( $plugins_page, 'append_licence_link_to_auto_update_unavailable_text' ), |
| 115 | 10, |
| 116 | 2 |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Enqueue the JavaScript to handle the licence tab on the plugins.php page. |
| 122 | */ |
| 123 | protected function add_assets_hooks(): void { |
| 124 | |
| 125 | // Only load the JS on the plugin information modal for this plugin. |
| 126 | global $pagenow; |
| 127 | if ( 'plugin-install.php' !== $pagenow |
| 128 | || ! isset( $_GET['plugin'] ) |
| 129 | || sanitize_key( wp_unslash( $_GET['plugin'] ) !== $this->settings->get_plugin_slug() ) |
| 130 | ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | $assets = new Admin_Assets( |
| 135 | $this->api, |
| 136 | $this->settings, |
| 137 | ); |
| 138 | |
| 139 | add_action( 'admin_enqueue_scripts', array( $assets, 'enqueue_script' ) ); |
| 140 | add_action( 'admin_enqueue_scripts', array( $assets, 'enqueue_styles' ) ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Add hooks for handling AJAX requests from the plugins.php licence management tab. |
| 145 | */ |
| 146 | protected function add_rest_hooks(): void { |
| 147 | $rest = new Rest( $this->api, $this->settings ); |
| 148 | |
| 149 | add_action( 'rest_api_init', array( $rest, 'register_routes' ) ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Add hooks to register WP CLI commands. |
| 154 | */ |
| 155 | protected function add_cli_hooks(): void { |
| 156 | |
| 157 | $cli = new CLI( $this->api, $this->settings, $this->logger ); |
| 158 | |
| 159 | add_action( 'cli_init', array( $cli, 'register_commands' ) ); |
| 160 | } |
| 161 | } |