-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamnesty-petitions.php
209 lines (177 loc) · 5.88 KB
/
amnesty-petitions.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
declare( strict_types = 1 );
namespace Amnesty\Petitions;
/*
Plugin Name: Humanity Petitions
Plugin URI: https://github.com/amnestywebsite/humanity-petitions
Description: Enable petitions support, with interface for synchronising data to a CRM. CRM integrations not included.
Version: 1.1.1
Author: Amnesty International
Author URI: https://www.amnesty.org
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: aip
Domain Path: /languages
Network: true
Requires PHP: 8.2.0
Requires at least: 5.8.0
Tested up to: 6.4.5
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
require_once __DIR__ . '/includes/helpers.php';
require_once __DIR__ . '/includes/abstract-class-singleton.php';
require_once __DIR__ . '/includes/interface-adapter.php';
require_once __DIR__ . '/includes/interface-logger.php';
require_once __DIR__ . '/includes/abstract-class-logger.php';
require_once __DIR__ . '/includes/class-database-adapter.php';
require_once __DIR__ . '/includes/class-database-logger.php';
require_once __DIR__ . '/includes/class-error-handler.php';
require_once __DIR__ . '/includes/class-exception.php';
require_once __DIR__ . '/includes/class-logs-list-table.php';
require_once __DIR__ . '/includes/class-signatures-list-table.php';
require_once __DIR__ . '/includes/class-post-type-petition.php';
require_once __DIR__ . '/includes/class-post-type-signatory.php';
require_once __DIR__ . '/includes/class-petition-handler.php';
require_once __DIR__ . '/includes/class-register-block.php';
require_once __DIR__ . '/includes/class-page-logs.php';
require_once __DIR__ . '/includes/class-page-settings.php';
require_once __DIR__ . '/includes/class-page-signatures.php';
require_once __DIR__ . '/includes/class-taxonomy-visibility.php';
require_once __DIR__ . '/includes/meta.php';
register_activation_hook(
__FILE__,
function () {
Database_Logger::up();
add_option( 'amnesty_petitions_settings', [ 'adapter' => Database_Adapter::class ] );
}
);
register_deactivation_hook(
__FILE__,
function () {
Database_Logger::down();
delete_option( 'amnesty_petitions_settings' );
delete_option( 'aip_petition_slug' );
}
);
/**
* Plugin instantiation class
*/
class Init {
/**
* Plugin data
*
* @var array
*/
protected $data = [];
/**
* Static reference to this file
*
* @var string
*/
public static $file = __FILE__;
/**
* Bind hooks and instantiate pages
*/
public function __construct() {
$this->data = get_plugin_data( __FILE__ );
add_filter( 'register_translatable_package', [ $this, 'register_translatable_package' ], 12 );
add_action( 'all_admin_notices', [ $this, 'check_dependencies' ] );
add_action( 'plugins_loaded', [ $this, 'textdomain' ] );
add_action( 'init', [ $this, 'boot' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
new Page_Settings( $this::get_logger() );
new Page_Logs( $this::get_logger() );
new Page_Signatures();
new Taxonomy_Visibility();
}
/**
* Register this plugin as a translatable package
*
* @param array<int,array<string,string>> $packages existing packages
*
* @return array<int,array<string,string>>
*/
public function register_translatable_package( array $packages = [] ): array {
$packages[] = [
'id' => 'humanity-petitions',
'path' => realpath( __DIR__ ),
'pot' => realpath( __DIR__ ) . '/languages/aip.pot',
'domain' => 'aip',
];
return $packages;
}
/**
* Output warning & deactivate if dependent plugins aren't active
*
* @return void
*/
public function check_dependencies(): void {
if ( function_exists( 'cmb2_bootstrap' ) ) {
return;
}
$missing = 'CMB2';
$message = sprintf(
// translators: [admin] %s: list of missing plugins
__( 'The Amnesty International Petitions plugin requires these plugins to be active: %s', 'aip' ),
$missing
);
printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $message ) );
deactivate_plugins( plugin_basename( __FILE__ ), false, is_multisite() );
}
/**
* Register textdomain
*
* @return void
*/
public function textdomain(): void {
load_plugin_textdomain( 'aip', false, basename( __DIR__ ) . '/languages' );
}
/**
* Instantiate required classes
*
* @return void
*/
public function boot(): void {
new Post_Type_Petition();
new Post_Type_Signatory();
new Petition_Handler( $this::get_logger(), Error_Handler::instance() );
new Register_Block();
}
/**
* Enqueue assets for the settings page
*
* @return void
*/
public function enqueue_assets(): void {
// v no need for nonce verification
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$post_type = get_option( 'aip_petition_slug' ) ?: 'petition';
$get_type = sanitize_text_field( $_GET['post_type'] ?? '' );
$the_page = sanitize_text_field( $_GET['page'] ?? '' );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
if ( $post_type !== $get_type && 'config' !== $the_page ) {
return;
}
wp_enqueue_script( 'amnesty-petitions-settings', plugins_url( 'assets/petitions-options.js', __FILE__ ), [ 'jquery' ], $this->data['Version'], true );
}
/**
* Retrieve logger instance
*
* @return AbstractLogger
*/
public static function get_logger(): AbstractLogger {
$user_logger = apply_filters( 'amnesty_petitions_logger', Database_Logger::class );
if ( is_callable( "{$user_logger}::instance" ) && is_subclass_of( $user_logger, AbstractLogger::class ) ) {
return $user_logger::instance();
}
$logger = Database_Logger::instance();
$logger->error( sprintf( '%s should extend %s; defaulting to %s', $user_logger, AbstractLogger::class, get_class( $logger ) ) );
return $logger;
}
}
new Init();