| 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 |
|
| 48 |
$asset = include PLGGRP_PATH . 'js/install.asset.php'; |
| 49 |
wp_enqueue_script( 'plugin-groups-install', PLGGRP_URL . 'js/install.js', $asset['dependencies'], $asset['version'], true ); |
| 50 |
|
| 51 |
$data = array( |
| 52 |
'url' => rest_url( Plugin_Groups::$slug . '/add' ), |
| 53 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 54 |
); |
| 55 |
wp_add_inline_script( 'plugin-groups-install', 'var plgData = ' . wp_json_encode( $data ), 'before' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Add our actions to the bulk actions. |
| 60 |
* |
| 61 |
* @param array $actions Current array of actions. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function add_actions( $actions, $plugin ) { |
| 66 |
// @todo: Clean up to use the HMTL builder. |
| 67 |
$groups = $this->plugin_groups->get_groups(); |
| 68 |
$last = array_pop( $actions ); |
| 69 |
$newaction = array(); |
| 70 |
$newaction[] = '<select disabled=disabled data-plugin="' . $plugin['slug'] . '" style="width:120px;">'; |
| 71 |
$newaction[] = '<option value="_select">'; |
| 72 |
$newaction[] = __( 'Add to group', 'plugin-groups' ); |
| 73 |
$newaction[] = '</option>'; |
| 74 |
foreach ( $groups as $group ) { |
| 75 |
$newaction[] = '<option value="' . $group['id'] . '">'; |
| 76 |
$newaction[] = $group['name']; |
| 77 |
$newaction[] = '</option>'; |
| 78 |
} |
| 79 |
$newaction[] = '</select>'; |
| 80 |
|
| 81 |
$actions[] = implode( $newaction ); |
| 82 |
$actions[] = $last; |
| 83 |
|
| 84 |
return $actions; |
| 85 |
} |
| 86 |
} |
| 87 |
|