RecommendedAddons.php
1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace ACP\Check;
use AC\Admin\Page\Addons;
use AC\Capabilities;
use AC\Integration;
use AC\Integration\Filter;
use AC\IntegrationRepository;
use AC\Integrations;
use AC\Message;
use AC\Registrable;
use AC\Screen;
class RecommendedAddons
implements Registrable {
/**
* @var IntegrationRepository
*/
private $integration_repository;
public function __construct( IntegrationRepository $integration_repository ) {
$this->integration_repository = $integration_repository;
}
public function register() {
add_action( 'ac/screen', [ $this, 'register_notice' ] );
}
private function get_recommended_addons() {
return $this->integration_repository->find_all( [
IntegrationRepository::ARG_FILTER => [
new Filter\IsNotActive( is_multisite(), is_network_admin() ),
new Filter\IsPluginActive(),
],
] );
}
/**
* @param Screen $screen
*/
public function register_notice( Screen $screen ) {
if ( ! $screen->has_screen() || ! current_user_can( Capabilities::MANAGE ) || ! $screen->is_admin_screen( Addons::NAME ) ) {
return;
}
$recommended_addons = $this->get_recommended_addons();
if ( ! $recommended_addons->exists() ) {
return;
}
$notice = new Message\Notice( $this->get_message( $recommended_addons ) );
$notice
->set_type( Message::INFO )
->register();
}
/**
* @return string
*/
private function get_message( Integrations $integrations ) {
$titles = array_map( static function ( Integration $integration ) {
return sprintf( '<strong>%s</strong>', $integration->get_title() );
}, $integrations->all() );
return sprintf(
'%s %s',
_n( 'We recommend installing this integration add-on:', 'We recommend installing these integration add-ons:', $integrations->count(), 'codepress-admin-columns' ),
ac_helper()->string->enumeration_list( $titles, 'and' )
);
}
}