| 1 |
<?php |
| 2 |
/** |
| 3 |
* Extra Actions for Plugin Groups. |
| 4 |
* |
| 5 |
* @package plugin_groups |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Plugin_Groups; |
| 9 |
|
| 10 |
/** |
| 11 |
* Plugin_Groups Class. |
| 12 |
*/ |
| 13 |
class Extras { |
| 14 |
|
| 15 |
/** |
| 16 |
* The single instance of the class. |
| 17 |
* |
| 18 |
* @var Plugin_Groups |
| 19 |
*/ |
| 20 |
protected $plugin_groups; |
| 21 |
|
| 22 |
/** |
| 23 |
* Initiate the bulk_actions object. |
| 24 |
* |
| 25 |
* @param Plugin_Groups $plugin_groups The instance of the main plugin. |
| 26 |
*/ |
| 27 |
public function __construct( Plugin_Groups $plugin_groups ) { |
| 28 |
|
| 29 |
$this->plugin_groups = $plugin_groups; |
| 30 |
// Start hooks. |
| 31 |
$this->setup_hooks(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Setup and register WordPress hooks. |
| 36 |
*/ |
| 37 |
protected function setup_hooks() { |
| 38 |
|
| 39 |
add_action( 'load-plugin-install.php', array( $this, 'enqueue_script' ) ); |
| 40 |
add_filter( 'plugin_install_action_links', array( $this, 'add_actions' ), 10, 2 ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Enqueue our scripts and data for the bulk actions JS. |
| 45 |
*/ |
| 46 |
public function enqueue_script() { |
| 47 |
$manifest_path = PLGGRP_PATH . 'static/manifest.json'; |
| 48 |
if ( ! file_exists( $manifest_path ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$manifest = json_decode( file_get_contents( $manifest_path ), true ); |
| 53 |
if ( ! isset( $manifest['src/extras.js'] ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$js_path = PLGGRP_URL . 'static/' . $manifest['src/extras.js']['file']; |
| 58 |
wp_enqueue_script( 'plugin-groups-bulk', $js_path, [], PLGGRP_VERSION, true ); |
| 59 |
|
| 60 |
$data = [ |
| 61 |
'groups' => $this->plugin_groups->get_groups(), |
| 62 |
'url' => rest_url( Plugin_Groups::$slug . '/add' ), |
| 63 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 64 |
'siteID' => get_current_blog_id(), |
| 65 |
]; |
| 66 |
wp_add_inline_script( 'plugin-groups-bulk', 'var plgData = ' . wp_json_encode( $data ), 'before' ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Add our actions to the bulk actions. |
| 71 |
* |
| 72 |
* @param array $actions Current array of actions. |
| 73 |
* |
| 74 |
* @return array |
| 75 |
*/ |
| 76 |
public function add_actions( $actions, $plugin ) { |
| 77 |
// @todo: Clean up to use the HMTL builder. |
| 78 |
$groups = $this->plugin_groups->get_groups(); |
| 79 |
$last = array_pop( $actions ); |
| 80 |
$newaction = array(); |
| 81 |
$newaction[] = '<select disabled=disabled data-plugin="' . esc_attr( $plugin['slug'] ) . '" style="width:120px;">'; |
| 82 |
$newaction[] = '<option value="_select">'; |
| 83 |
$newaction[] = __( 'Add to group', 'plugin-groups' ); |
| 84 |
$newaction[] = '</option>'; |
| 85 |
foreach ( $groups as $group ) { |
| 86 |
$newaction[] = '<option value="' . esc_attr( $group['id'] ) . '">'; |
| 87 |
$newaction[] = esc_html( $group['name'] ); |
| 88 |
$newaction[] = '</option>'; |
| 89 |
} |
| 90 |
$newaction[] = '</select>'; |
| 91 |
|
| 92 |
$actions[] = implode( $newaction ); |
| 93 |
$actions[] = $last; |
| 94 |
|
| 95 |
return $actions; |
| 96 |
} |
| 97 |
} |
| 98 |
|