Addons.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Admin\Main; |
| 4 | |
| 5 | use AC; |
| 6 | use AC\Admin; |
| 7 | use AC\Asset\Assets; |
| 8 | use AC\Asset\Enqueueables; |
| 9 | use AC\Asset\Location; |
| 10 | use AC\Asset\Style; |
| 11 | use AC\PluginInformation; |
| 12 | use AC\Renderable; |
| 13 | |
| 14 | class Addons implements Enqueueables, Renderable { |
| 15 | |
| 16 | const NAME = 'addons'; |
| 17 | |
| 18 | /** |
| 19 | * @var Location\Absolute |
| 20 | */ |
| 21 | private $location; |
| 22 | |
| 23 | /** |
| 24 | * @var AC\Integrations |
| 25 | */ |
| 26 | private $integrations; |
| 27 | |
| 28 | public function __construct( Location\Absolute $location, AC\Integrations $integrations ) { |
| 29 | $this->location = $location; |
| 30 | $this->integrations = $integrations; |
| 31 | } |
| 32 | |
| 33 | public function get_assets() { |
| 34 | return new Assets( [ |
| 35 | new Style( 'ac-admin-page-addons', $this->location->with_suffix( 'assets/css/admin-page-addons.css' ) ), |
| 36 | new Admin\Asset\Addons( 'ac-admin-page-addons', $this->location->with_suffix( 'assets/js/admin-page-addons.js' ) ), |
| 37 | ] ); |
| 38 | } |
| 39 | |
| 40 | public function render() { |
| 41 | ob_start(); |
| 42 | |
| 43 | foreach ( $this->get_grouped_addons() as $group ) : |
| 44 | ?> |
| 45 | |
| 46 | <div class="ac-addons group-<?= esc_attr( $group['class'] ); ?>"> |
| 47 | <h2><?php echo esc_html( $group['title'] ); ?></h2> |
| 48 | |
| 49 | <ul> |
| 50 | <?php |
| 51 | foreach ( $group['integrations'] as $addon ) { |
| 52 | /* @var AC\Integration $addon */ |
| 53 | |
| 54 | $view = new AC\View( [ |
| 55 | 'logo' => AC()->get_url() . $addon->get_logo(), |
| 56 | 'title' => $addon->get_title(), |
| 57 | 'slug' => $addon->get_slug(), |
| 58 | 'description' => $addon->get_description(), |
| 59 | 'actions' => $this->render_actions( $addon ), |
| 60 | ] ); |
| 61 | |
| 62 | echo $view->set_template( 'admin/edit-addon' ); |
| 63 | } |
| 64 | ?> |
| 65 | </ul> |
| 66 | </div> |
| 67 | <?php endforeach; |
| 68 | |
| 69 | return ob_get_clean(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param AC\Integration $addon |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | private function render_actions( AC\Integration $addon ) { |
| 78 | ob_start(); |
| 79 | |
| 80 | $plugin = new PluginInformation( $addon->get_basename() ); |
| 81 | $view = new Admin\AddonStatus( $plugin, $addon, is_multisite(), is_network_admin() ); |
| 82 | $view->render(); |
| 83 | |
| 84 | return ob_get_clean(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return array |
| 89 | */ |
| 90 | private function get_grouped_addons() { |
| 91 | |
| 92 | $active = []; |
| 93 | $recommended = []; |
| 94 | $available = []; |
| 95 | |
| 96 | foreach ( $this->integrations->all() as $integration ) { |
| 97 | $plugin = new PluginInformation( $integration->get_basename() ); |
| 98 | |
| 99 | $is_active = $plugin->is_network_active() |
| 100 | || ( ! is_multisite() && $plugin->is_active() ) |
| 101 | || ( is_multisite() && ! is_network_admin() && $plugin->is_active() ); |
| 102 | |
| 103 | if ( $is_active ) { |
| 104 | $active[] = $integration; |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | if ( $integration->is_plugin_active() ) { |
| 109 | $recommended[] = $integration; |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | $available[] = $integration; |
| 114 | } |
| 115 | |
| 116 | $groups = []; |
| 117 | |
| 118 | if ( $recommended ) { |
| 119 | $groups[] = [ |
| 120 | 'title' => __( 'Recommended', 'codepress-admin-columns' ), |
| 121 | 'class' => 'recommended', |
| 122 | 'integrations' => $recommended, |
| 123 | ]; |
| 124 | } |
| 125 | |
| 126 | if ( $active ) { |
| 127 | $groups[] = [ |
| 128 | 'title' => __( 'Active', 'codepress-admin-columns' ), |
| 129 | 'class' => 'active', |
| 130 | 'integrations' => $active, |
| 131 | ]; |
| 132 | } |
| 133 | |
| 134 | if ( $available ) { |
| 135 | $groups[] = [ |
| 136 | 'title' => __( 'Available', 'codepress-admin-columns' ), |
| 137 | 'class' => 'available', |
| 138 | 'integrations' => $available, |
| 139 | ]; |
| 140 | } |
| 141 | |
| 142 | return $groups; |
| 143 | } |
| 144 | |
| 145 | } |