PluginProbe
Plugin Load Filter / 4.0.6
Plugin Load Filter v4.0.6
trunk 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.3.1 2.4.0 2.4.1 2.5.1 3.3.0 4.0.6 4.1.1 4.2.0 4.3.0 4.3.1 4.4.0
plugin-load-filter / plugin-load-filter.php

plugin-load-filter.php in Plugin Load Filter 4.0.6, at plugin-load-filter.php

1,265 lines 66.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: plugin load filter
4 Description: Dynamically activate the selected plugins for each page. Response will be faster by filtering plugins.
5 Version: 4.0.6
6 Plugin URI: https://celtislab.net/en/wp-plugin-load-filter
7 Author: enomoto@celtislab
8 Author URI: https://celtislab.net/
9 License: GPLv2
10 Text Domain: plf
11 Domain Path: /languages
12 */
13 defined( 'ABSPATH' ) || exit;
14
15 /***************************************************************************
16 * plugin activation / deactivation / uninstall
17 **************************************************************************/
18 if(is_admin()){
19 //deactivation
20 function plugin_load_filter_deactivation( $network_deactivating ) {
21 $act = false;
22 if (is_multisite()) {
23 if(! $network_deactivating){
24 global $wpdb;
25 $current_blog_id = get_current_blog_id();
26 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
27 foreach ( $blog_ids as $blog_id ) {
28 if($blog_id == $current_blog_id){
29 //current site
30 }
31 else {
32 //other site active check
33 switch_to_blog( $blog_id );
34 if ( is_plugin_active( plugin_basename( __FILE__ )))
35 $act = true;
36 }
37 }
38 switch_to_blog( $current_blog_id );
39 }
40 }
41 if($act === false){
42 flush_rewrite_rules(); //options data 'rewrite_rules' clear for remake.
43 if ( file_exists( WPMU_PLUGIN_DIR . "/plf-filter.php" )) {
44 @unlink( WPMU_PLUGIN_DIR . '/plf-filter.php' );
45 }
46 }
47 }
48 register_deactivation_hook( __FILE__, 'plugin_load_filter_deactivation' );
49
50 //uninstall
51 function plugin_load_filter_uninstall() {
52
53 if ( !is_multisite()) {
54 delete_option('plf_queryvars');
55 delete_option('plf_option' );
56 delete_option('plf_addon_options');
57 } else {
58 global $wpdb;
59 $current_blog_id = get_current_blog_id();
60 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
61 foreach ( $blog_ids as $blog_id ) {
62 switch_to_blog( $blog_id );
63 delete_option('plf_queryvars');
64 delete_option('plf_option' );
65 delete_option('plf_addon_options');
66 }
67 switch_to_blog( $current_blog_id );
68 }
69 if ( file_exists( WPMU_PLUGIN_DIR . "/plf-filter.php" )) {
70 @unlink( WPMU_PLUGIN_DIR . '/plf-filter.php' );
71 }
72 }
73 register_uninstall_hook(__FILE__, 'plugin_load_filter_uninstall');
74
75 }
76
77 $Plf_setting = new Plf_setting();
78
79 class Plf_setting {
80
81 static $plugins_inf = ''; //active plugin/module infomation
82 static $filter = array(); //filter option data
83 private $tab_num = 0;
84
85 /***************************************************************************
86 * Style Sheet
87 **************************************************************************/
88 function plf_css() { ?>
89 <style type="text/css">
90 #plugin-filter-select {margin-top: 12px;}
91 #plugin-filter-select p {margin: 1em 0;}
92 #page-filter-stat { margin-top: 12px;}
93 #activation-table { padding-bottom: 8px; border: 1px solid #eee;}
94 #activation-table th { text-align: center;}
95 #activation-table td { font-size: 97%;}
96 #activation-table input[type=checkbox] { height: 25px; width: 25px; opacity: 0;}
97 #plf-post-locale-select { margin-top: 16px;}
98 thead .plugins-name { background-color: aliceblue;}
99 thead .device-type { background-color: oldlace;}
100 thead .plugins-name, tbody .plugins-name { min-width: 144px; max-width: 200px; padding: 3px 3px 3px 5px;}
101 thead .device-type, tbody .device-type { min-width: 40px; max-width: 40px; text-align: center; padding: 5px 1px 2px;}
102 .dashicons-yes:before { font-size: 20px; border: 1px solid #eee; background-color: whitesmoke;}
103 .device-type label { color: whitesmoke; }
104 input.altcheckbox[type="checkbox"] { -webkit-appearance: none; appearance: none; position: absolute;}
105 .device-type input.altcheckbox[type="checkbox"]:checked + span.dashicons-yes:before { background-color: yellowgreen; }
106 .plf-option-info { font-size:12px; margin:10px 0; padding:3px; background-color:#fff8e5;}
107 .edit-post-locale-link span.dashicons { width: 0.8em; height: 0.8em; margin: -0.4em 0.1em 0; vertical-align: middle; text-decoration: initial;}
108 </style>
109 <?php }
110
111 function jquery_tab_css() { ?>
112 <style type="text/css">
113 .ui-helper-reset { margin: 0; padding: 0; border: 0; outline: 0; line-height: 1.5; text-decoration: none; font-size: 100%; list-style: none; }
114 .ui-helper-clearfix:before, .ui-helper-clearfix:after { content: ""; display: table; }
115 .ui-helper-clearfix:after { clear: both; }
116 .ui-helper-clearfix { zoom: 1; }
117 .ui-tabs { position: relative; padding: .2em; zoom: 1; } /* position: relative prevents IE scroll bug (element with position: relative inside container with overflow: auto appear as "fixed") */
118 .ui-tabs .ui-tabs-nav { margin: 1px 8px; padding: .2em .2em; }
119 .ui-tabs .ui-tabs-nav li { list-style: none; float: left; position: relative; top: 0; margin: 1px .3em 0 0; border-bottom: 0; padding: 0; white-space: nowrap; }
120 .ui-tabs .ui-tabs-nav li a { float: left; text-decoration: none; }
121 .ui-tabs .ui-tabs-nav li.ui-tabs-active { margin-bottom: -1px; padding-bottom: 1px; }
122 .ui-tabs .ui-tabs-panel { display: block; border-width: 0; background: none; }
123 .ui-tabs .ui-tabs-nav a { margin: 8px 10px; }
124 .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default { border: 1px solid #dddddd; background-color: #f4f4f4; font-weight: bold; color: #0073ea; }
125 .ui-state-default a, .ui-state-default a:link, .ui-state-default a:visited { color: #0073ea; text-decoration: none; }
126 .ui-state-hover, .ui-widget-content .ui-state-hover, .ui-widget-header .ui-state-hover, .ui-state-focus, .ui-widget-content .ui-state-focus,.ui-widget-header .ui-state-focus { border: 1px solid #0073ea; background-color: #0073ea; font-weight: bold; color: #ffffff; }
127 .ui-state-active, .ui-widget-content .ui-state-active, .ui-widget-header .ui-state-active { border: 1px solid #dddddd; background-color: #0073ea; font-weight: bold; color: #ffffff; }
128 .ui-state-hover a, .ui-state-hover a:hover, .ui-state-hover a:link, .ui-state-hover a:visited { color: #ffffff; text-decoration: none; }
129 .ui-state-active a, .ui-state-active a:link, .ui-state-active a:visited { color: #ffffff; text-decoration: none; }
130
131 #wrap_registration-table, #wrap_activation-table { overflow:auto; height:600px; position: relative;}
132 #registration-table input[type=radio], #registration-table input[type=checkbox], #activation-table input[type=checkbox] { height: 25px; width: 25px; opacity: 0;}
133 #registration-table th {text-align: center;}
134 thead, tbody { display: block;}
135 .widefat * { word-wrap: break-word !important;}
136 .widefat thead { position:sticky; top:0px; z-index:1;}
137 .widefat tr:first-of-type th:first-of-type {position: sticky; left: 0px; text-align: left; background-color: aliceblue; z-index:3;}
138 .widefat th { padding: 8px;}
139 .widefat td { padding: 8px;}
140 .widefat td:first-of-type { position: sticky; left: 0px; text-align: left; background-color: white;}
141 thead .filter-plugins-name, thead .plugins-name { background-color: aliceblue;}
142 thead .urlfilter { background-color: lavenderblush;}
143 .filter-none, .filter-admin, .filter-tmpl { background-color: honeydew;}
144 thead .device-type { background-color: oldlace;}
145 thead .ckbox-type { background-color: lavender;}
146 thead .pformat { background-color: honeydew;}
147 thead .tmpl-embed { background-color: lightyellow;}
148 thead .tmpl-custom { background-color: lavenderblush;}
149 thead .filter-plugins-name, tbody .filter-plugins-name { min-width: 240px; max-width: 240px;}
150 thead .plugins-name, tbody .plugins-name { min-width: 180px; max-width: 180px;}
151 thead .device-type, tbody .device-type, thead .ckbox-type, tbody .ckbox-type, thead .deny-type, tbody .deny-type { min-width: 40px; max-width: 40px; text-align: center;}
152 thead .filter-type, tbody .filter-type { min-width: 56px; max-width: 56px; text-align:center;}
153 .urlfilter-description { padding: 0 10px 15px;}
154 .grid-row { display: flex; flex-flow: row wrap;}
155 .filter-description { padding: 0 10px; width:62%;}
156 .side-info { width: 30%; padding-left: 24px;}
157 .exclude-pformat { padding: 5px 0 20px}
158 .exclude-pformat label { white-space:nowrap;}
159 .exclude-pformat span { margin-right: 12px; }
160 .dashicons:before { font-size: 24px; }
161 .radio-green label, .radio-red label { color: #ddd; margin-left: -32px; }
162 .ckbox-type label { color: #ddd;}
163 .radio-green input[type="radio"]:checked + span { color: #8bc34a; }
164 .radio-red input[type="radio"]:checked + span { color: tomato; }
165 .ckbox-type input[type="checkbox"]:checked + span { color: #4caf50; }
166 /* .dashicons-dismiss:before { background-color: yellowgreen; font-size: 20px; border-radius: 12px; } */
167 .deny-type label { color: #4caf50;}
168 .deny-type input.altcheckbox[type="checkbox"]:checked + span { color: #ddd; }
169 .dashicons-yes:before { font-size: 20px; border: 1px solid #eee; }
170 .device-type input.altcheckbox[type="checkbox"]:checked + span.dashicons-yes:before { background-color: yellowgreen; }
171 .language-option { margin-bottom: 28px; }
172 </style>
173 <?php }
174
175 /***************************************************************************
176 * Plugin Load Filter Option Setting
177 **************************************************************************/
178
179 public function __construct() {
180
181 load_plugin_textdomain('plf', false, basename( dirname( __FILE__ ) ).'/languages' );
182
183 self::$filter = get_option('plf_option');
184 if(empty(self::$filter['optver']) || self::$filter['optver'] < '2'){
185 self::$filter['optver'] = '2';
186 //ここにデータフォーマットが変わった場合の変換処理を記述
187 }
188 if(!empty(self::$filter['language'])){
189 add_action( 'wp_head', array('Plf_setting', 'altenate_hreflang') );
190 }
191 if(is_admin()) {
192 add_action( 'plugins_loaded', array(&$this, 'plf_admin_start'), 9999 );
193 add_action( 'admin_init', array(&$this, 'action_posts'));
194 add_action( 'add_meta_boxes', array(&$this, 'load_meta_boxes'), 10, 2 );
195 }
196 add_action( 'wp_ajax_plugin_load_filter', array(&$this, 'plf_ajax_postidfilter'));
197 }
198
199 //Plugin Load Filter admin setting start
200 public function plf_admin_start() {
201 require_once(ABSPATH . 'wp-admin/includes/plugin.php');
202
203 self::$plugins_inf = get_plugins();
204 //Ver4.0.0 でしか呼び出せないメソッドを呼び出さないよう新設した switch_theme があるかどうかで判定
205 if(method_exists('Plf_filter', 'switch_theme')){
206 if(class_exists('Plf_filter')){
207 if ( is_multisite() )
208 remove_filter('pre_site_option_active_sitewide_plugins', array('Plf_filter', 'active_sitewide_plugins'));
209 remove_filter('pre_option_active_plugins', array('Plf_filter', 'active_plugins'));
210 }
211 foreach ( self::$plugins_inf as $plugin_key => $a_plugin ) {
212 if(is_plugin_inactive( $plugin_key )){
213 unset(self::$plugins_inf[$plugin_key]);
214 }
215 }
216 if(class_exists('Plf_filter')){
217 if ( is_multisite() )
218 add_filter('pre_site_option_active_sitewide_plugins', array('Plf_filter', 'active_sitewide_plugins'));
219 add_filter('pre_option_active_plugins', array('Plf_filter', 'active_plugins'));
220 }
221
222 //jetpack active module
223 if(method_exists('Jetpack', 'get_module')){
224 if(class_exists('Plf_filter')){
225 remove_filter('pre_option_jetpack_active_modules', array('Plf_filter', 'active_jetmodules'));
226 }
227 $modules = Jetpack::get_active_modules();
228 $modules = array_diff( $modules, array( 'vaultpress' ) );
229 foreach ( $modules as $key => $module_name ) {
230 if(!empty($module_name)){
231 $module = Jetpack::get_module( $module_name );
232 if(!empty($module))
233 self::$plugins_inf['jetpack_module/' . $module_name] = $module;
234 }
235 }
236 if(class_exists('Plf_filter')){
237 add_filter('pre_option_jetpack_active_modules', array('Plf_filter', 'active_jetmodules'));
238 }
239 }
240 //celtispack active module
241 if(method_exists('Celtispack', 'get_module')){
242 if(class_exists('Plf_filter')){
243 remove_filter('pre_option_celtispack_active_modules', array('Plf_filter', 'active_celtismodules'));
244 }
245 $modules = Celtispack::get_active_modules();
246 foreach ( $modules as $key => $module_name ) {
247 if(!empty($module_name)){
248 self::$plugins_inf['celtispack_module/' . $module_name] = Celtispack::get_module( $module_name );
249 }
250 }
251 if(class_exists('Plf_filter')){
252 add_filter('pre_option_celtispack_active_modules', array('Plf_filter', 'active_celtismodules'));
253 }
254 }
255 if ( empty( self::$plugins_inf ) )
256 return;
257 }
258
259 add_action('admin_menu', array(&$this, 'plf_option_menu'));
260 }
261
262 static function get_plugins_inf() {
263 $inf = (!empty(self::$plugins_inf))? self::$plugins_inf : false;
264 return $inf;
265 }
266
267 //Plugins sub menu add
268 public function plf_option_menu() {
269 if(current_user_can( 'activate_plugins' )){
270 $page = add_menu_page( 'Plugin Load FIlter Settings', __('Plugin Load Filter', 'plf'), 'manage_options', 'plugin_load_filter_admin_manage_page', array(&$this, 'plf_option_page'), 'dashicons-filter', '65.1');
271 add_submenu_page( 'plugin_load_filter_admin_manage_page', 'Plugin Load FIlter Settings', __('General Settings', 'plf'), 'manage_options', 'plugin_load_filter_admin_manage_page', array(&$this, 'plf_option_page') );
272 add_action( 'admin_print_scripts-'.$page, array(&$this, 'plf_scripts') );
273 add_action( 'admin_print_scripts-'.$page, array(&$this, 'deploy_mu_plugins'));
274 }
275 }
276
277 //Plugin Load Filter setting page script
278 function plf_scripts() {
279 wp_enqueue_script( 'jquery' );
280 wp_enqueue_script( 'jquery-ui-core' );
281 wp_enqueue_script( 'jquery-ui-widget' );
282 wp_enqueue_script( 'jquery-ui-tabs' );
283 add_action( 'admin_head', array(&$this, 'plf_css' ));
284 add_action( 'admin_head', array(&$this, 'jquery_tab_css' ));
285 add_action( 'admin_footer', array(&$this, 'activetab_script' ));
286 add_action( 'admin_notices', array('Plf_setting', 'plf_notice'));
287 }
288
289 //plf-filter.php mu-plugins module set
290 public function deploy_mu_plugins() {
291 if(wp_mkdir_p( WPMU_PLUGIN_DIR )){
292 if ( !file_exists( WPMU_PLUGIN_DIR . "/plf-filter.php" )) {
293 @copy(__DIR__ . '/mu-plugins/plf-filter.php', WPMU_PLUGIN_DIR . '/plf-filter.php');
294 }
295 else {
296 require_once(ABSPATH . 'wp-admin/includes/plugin.php');
297 $dp = get_plugin_data( WPMU_PLUGIN_DIR . "/plf-filter.php", false, false );
298 $sp = get_plugin_data( __DIR__ . '/mu-plugins/plf-filter.php', false, false );
299 if(version_compare( $dp['Version'], $sp['Version'], '!=')){
300 @copy(__DIR__ . '/mu-plugins/plf-filter.php', WPMU_PLUGIN_DIR . '/plf-filter.php');
301 }
302 }
303 }
304 }
305
306 //Notice Message display
307 static function plf_notice() {
308 $notice = get_transient('plf_notice');
309 if(!empty($notice)){
310 echo '<div class="notice notice-warning"><p>Plugin Load Filter : ' . $notice . '</p></div>';
311 delete_transient('plf_notice');
312 }
313 }
314
315 //plugin filter option action request (add, update, delete)
316 function action_posts() {
317 if (current_user_can( 'activate_plugins' )) {
318 if( isset($_POST['edit_regist_filter']) ) {
319 if(isset($_POST['plfregist'])){
320 check_admin_referer('plugin_load_filter');
321 //url filter
322 $groupkeys = (method_exists('Plf_filter', 'get_active_group'))? Plf_filter::get_active_group() : array();
323 if(!empty($groupkeys)){
324 $urlkeys = array_keys($_POST['plfurlkey']);
325 foreach( $groupkeys as $item){
326 if(empty( $_POST['plfurlkey'][$item])){
327 self::$filter['plfurlkey'][$item]['plugins'] = '';
328 } else {
329 $plugins = array();
330 foreach ( $_POST['plfurlkey'][$item] as $p_key => $val ) {
331 if($val == '1')
332 $plugins[] = $p_key;
333 }
334 $option["plugins"] = implode(",", $plugins);
335 self::$filter['plfurlkey'][$item] = $option;
336 }
337 }
338 }
339 //page type filter
340 foreach( array('_admin', '_pagefilter') as $item){
341 $plugins = array();
342 foreach ( $_POST['plfregist'] as $p_key => $val ) {
343 if($val == $item)
344 $plugins[$p_key] = $val;
345 }
346 if($item == '_pagefilter'){
347 //If all modules is specified filter, in some cases you want to deactivate plugin itself.
348 $jbase = $cbase = '';
349 $jall = $call = true;
350 foreach ( $_POST['plfregist'] as $p_key => $val ) {
351 if(strpos($p_key, 'jetpack/') !== false)
352 $jbase = $p_key;
353 else if(strpos($p_key, 'celtispack/') !== false)
354 $cbase = $p_key;
355 else if(strpos($p_key, 'jetpack_module/') !== false){
356 if($val != '_pagefilter' && $val != '_admin'){
357 $jall = false;
358 }
359 }
360 else if(strpos($p_key, 'celtispack_module/') !== false){
361 if($val != '_pagefilter' && $val != '_admin')
362 $call = false;
363 }
364 }
365 if(!empty($jbase) && $jall === false)
366 unset($plugins[$jbase]);
367 if(!empty($cbase) && $call === false)
368 unset($plugins[$cbase]);
369 }
370 $option["plugins"] = implode(",", array_keys($plugins));
371 self::$filter[$item] = $option;
372 }
373 //exclude option
374 if(isset($_POST['plf_option']['exclude'])){
375 $exclude = array();
376 foreach ( $_POST['plf_option']['exclude'] as $ft => $v ) {
377 if(!empty($v))
378 $exclude[$ft] = true;
379 }
380 self::$filter['exclude'] = $exclude;
381 } else {
382 self::$filter['exclude'] = array();
383 }
384
385 //admin bar (filtered stat)
386 self::$filter['admin_bar'] = (isset($_POST['plf_option']['admin_bar']))? 1 : 0;
387
388 //Language option
389 self::$filter['language'] = (isset($_POST['plf_option']['language']))? 1 : 0;
390
391 update_option('plf_option', self::$filter );
392 }
393 header('Location: ' . admin_url('plugins.php?page=plugin_load_filter_admin_manage_page'));
394 exit;
395
396 } elseif( isset($_POST['clear_regist_filter']) ) {
397 check_admin_referer('plugin_load_filter');
398 if(!empty(self::$filter['plfurlkey'])){
399 foreach (self::$filter['plfurlkey'] as $key => $val) {
400 unset(self::$filter['plfurlkey'][$key]);
401 }
402 }
403 foreach( array('_admin', '_pagefilter') as $item){
404 self::$filter[$item] = array();
405 }
406 self::$filter['exclude'] = array();
407
408 //old data unset
409 if(isset(self::$filter['urlkey'])){
410 unset(self::$filter['urlkey']);
411 }
412 if(isset(self::$filter['urlkeylist'])){
413 unset(self::$filter['urlkeylist']);
414 }
415
416 self::$filter['admin_bar'] = 0;
417 self::$filter['language'] = 0;
418 update_option('plf_option', self::$filter );
419 header('Location: ' . admin_url('plugins.php?page=plugin_load_filter_admin_manage_page'));
420 exit;
421
422 } else if(isset($_POST['edit_activate_page_filter']) ) {
423 if(isset($_POST['plfactive'])){
424 check_admin_referer('plugin_load_filter');
425 self::$filter['group'] = array();
426 $group = array_keys($_POST['plfactive']);
427 foreach( $group as $item){
428 $plugins = array();
429 foreach ( $_POST['plfactive'][$item] as $p_key => $val ) {
430 if($val == '1')
431 $plugins[] = $p_key;
432 }
433 $option["plugins"] = implode(",", $plugins);
434 self::$filter['group'][$item] = $option;
435 }
436 update_option('plf_option', self::$filter );
437 }
438 header('Location: ' . admin_url('plugins.php?page=plugin_load_filter_admin_manage_page&action=tab_1'));
439 exit;
440
441 } elseif( isset($_POST['clear_activate_page_filter']) ) {
442 check_admin_referer('plugin_load_filter');
443 self::$filter['group'] = array();
444 update_option('plf_option', self::$filter );
445 header('Location: ' . admin_url('plugins.php?page=plugin_load_filter_admin_manage_page&action=tab_1'));
446 exit;
447 }
448 if(!empty($_GET['action']) && $_GET['action']=='tab_1') {
449 $this->tab_num = 1;
450 }
451 }
452 }
453
454 //Plugin or Module key to name
455 // $type : list/smart/tree
456 static function pluginkey_to_name( $infkey, $type='list') {
457 $name = '';
458 if(strpos($infkey, 'jetpack_module/') !== false){
459 if(!empty(self::$plugins_inf[$infkey]['name'])){
460 $m_mark = ($type !== 'list')? '-' : 'Jetpack-';
461 if($type === 'smart') {
462 if(empty(self::$filter['_pagefilter']['plugins']) || strpos(self::$filter['_pagefilter']['plugins'], 'jetpack/') === false)
463 $name = $m_mark . self::$plugins_inf[$infkey]['name'];
464 } else {
465 $name = $m_mark . self::$plugins_inf[$infkey]['name'];
466 }
467 }
468 } elseif(strpos($infkey, 'celtispack_module/') !== false){
469 if(!empty(self::$plugins_inf[$infkey]['Name'])){
470 $m_mark = ($type !== 'list')? '-' : 'Celtispack-';
471 if($type === 'smart') {
472 if(empty(self::$filter['_pagefilter']['plugins']) || strpos(self::$filter['_pagefilter']['plugins'], 'celtispack/') === false)
473 $name = $m_mark . self::$plugins_inf[$infkey]['Name'];
474 } else {
475 $name = $m_mark . self::$plugins_inf[$infkey]['Name'];
476 }
477 }
478 } else {
479 if(!empty(self::$plugins_inf[$infkey]['Name']))
480 $name = self::$plugins_inf[$infkey]['Name'];
481 }
482 return($name);
483 }
484
485 //Checkbox
486 static function checkbox($name, $value, $label = '') {
487 return "<label><input type='checkbox' name='$name' value='1' " . checked( $value, 1, false ). "/> $label</label>";
488 }
489 static function altcheckbox($name, $value, $label = '') {
490 //return "<input type='hidden' name='$name' value='0'><input type='checkbox' name='$name' value='1' " . checked( $value, 1, false ). "/><label> $label</label>";
491 return "<label><input type='checkbox' name='$name' class='altcheckbox' value='1' " . checked( $value, 1, false ). "/> $label</label>";
492 }
493
494 //dropdown list
495 static function dropdown($name, $items, $selected, $args = null) {
496 $defaults = array(
497 'id' => $name,
498 'none' => false,
499 'class' => null,
500 'multiple' => false,
501 'select_attr' => ""
502 );
503
504 if (!is_array($items))
505 return;
506
507 if (empty($items))
508 $items = array();
509
510 // Items is in key => value format. If value is itself an array, use only the 1st column
511 foreach ($items as $key => &$value) {
512 if (is_array($value))
513 $value = array_shift($value);
514 }
515
516 extract(wp_parse_args($args, $defaults));
517
518 // If 'none' arg provided, prepend a blank entry
519 if ($none) {
520 if ($none === true)
521 $none = '&nbsp;';
522 $items = array('' => $none) + $items; // Note that array_merge() won't work because it renumbers indexes!
523 }
524
525 if (!$id)
526 $id = $name;
527
528 $name = ($name) ? "name='$name'" : "";
529 $id = ($id) ? "id='$id'" : "";
530 $class = ($class) ? "class='$class'" : "";
531 $multiple = ($multiple) ? "multiple='multiple'" : "";
532
533 $html = "<select $name $id $class $multiple $select_attr>";
534
535 foreach ((array) $items as $key => $label) {
536 $key = esc_attr($key);
537 $label = esc_attr($label);
538
539 $html .= "<option value='$key' " . selected($selected, $key, false) . ">$label</option>";
540 }
541 $html .= "</select>";
542 return $html;
543 }
544
545 public function plfregist_item($key, $chklist, $filter) {
546 $p_name = self::pluginkey_to_name($key);
547 $opt_name = "plfregist[$key]";
548 ?>
549 <tr id="plfregist_<?php echo $key; ?>">
550 <td class="filter-plugins-name"><?php echo $p_name; ?></td>
551 <?php
552 foreach($chklist as $urlkey){
553 $checked = (empty($filter['plfurlkey'][$urlkey]['plugins']) || false === strpos($filter['plfurlkey'][$urlkey]['plugins'], $key))? false : true;
554 echo '<td class="deny-type filter-type">' . self::altcheckbox("plfurlkey[$urlkey][$key]", $checked, '<span class="dashicons dashicons-admin-plugins"></span>') . '</td>';
555 }
556 $radio = '';
557 if(!empty($filter['_admin']['plugins']) && false !== strpos($filter['_admin']['plugins'], $key))
558 $radio = '_admin';
559 else if(!empty($filter['_pagefilter']['plugins']) && false !== strpos($filter['_pagefilter']['plugins'], $key))
560 $radio = '_pagefilter';
561 ?>
562 <td class="radio-green filter-type"><label><input type="radio" name="<?php echo $opt_name; ?>" value='' <?php checked('', $radio); ?>/><span class="dashicons dashicons-admin-plugins"></span></label></td>
563 <td class="radio-red filter-type"><label><input type="radio" name="<?php echo $opt_name; ?>" value="_admin" <?php checked('_admin', $radio); ?>/><span class="dashicons dashicons-admin-plugins"></span></label></td>
564 <td class="radio-red filter-type"><label><input type="radio" name="<?php echo $opt_name; ?>" value="_pagefilter" <?php checked('_pagefilter', $radio); ?>/><span class="dashicons dashicons-admin-plugins"></span></label></td>
565 </tr>
566 <?php
567 }
568
569 //Filterring plugins select
570 public function plfregist_table($plugins, $filter) {
571 ?>
572 <div id="wrap_registration-table">
573 <table id="registration-table" class="widefat">
574 <thead>
575 <?php
576 $groupkeys = (method_exists('Plf_filter', 'get_active_group'))? Plf_filter::get_active_group() : array();
577 $urlnum = count($groupkeys);
578 ?>
579 <tr>
580 <th class="filter-plugins-name" rowspan="2"><?php _e('Plugins'); ?></th>
581 <?php if($urlnum > 0) { ?>
582 <th class="filter-type urlfilter" colspan="<?php echo $urlnum; ?>" style="font-weight:bold; font-size:smaller;"><?php _e('URL Group Filter', 'plf'); ?></th>
583 <?php } ?>
584 <th class="filter-type filter-tmpl" colspan="3" style="font-weight:bold; font-size:smaller;"><?php _e('Page Type Filter', 'plf'); ?></th>
585 </tr>
586 <tr>
587 <?php //Addon URL filter group key
588 if($urlnum > 0) {
589 foreach ( $groupkeys as $v) {
590 $_flt = (method_exists('Plf_filter', 'get_slug_filter'))? Plf_filter::get_slug_filter( $v ) : array();
591 $hint = '';
592 foreach ($_flt as $_v) {
593 $hint .= '[' . $_v['slug'] . '] ' . $_v['summary'] . PHP_EOL;
594
595 if((int)$_v['targetpage'] === 1) {
596 $post_type = (!empty($_v['post_type']))? $_v['post_type'] : '';
597 $taxonomy = '';
598 if(!empty($_v['taxonomies']) && !empty($_v['term'])){
599 $taxonomy = " : {$_v['taxonomies']}({$_v['term']})";
600 }
601 if(!empty($post_type)){
602 $hint .= 'Singular (' . $post_type . ')' . $taxonomy . PHP_EOL;
603 } else {
604 $hint .= 'Singular ' . $taxonomy . PHP_EOL;
605 }
606 } elseif((int)$_v['targetpage'] === 2) {
607 if(!empty($_v['ajax_action'])){
608 $hint .= 'Ajax request (' . $_v['ajax_action'] . ')' . PHP_EOL;
609 } else {
610 $hint .= 'Ajax request' . PHP_EOL;
611 }
612 }
613
614 $hint .= 'url filter : ';
615 $urlkey = (!empty($_v['url_path']))? array_filter( array_map("trim", explode(PHP_EOL, $_v['url_path']))) : array();
616 if(!empty($urlkey)){
617 foreach ($urlkey as $key) {
618 $hint .= $key . ' ';
619 }
620 }
621 $urlkey = (!empty($_v['url_q_and']))? array_filter( array_map("trim", explode(PHP_EOL, $_v['url_q_and']))) : array();
622 if(!empty($urlkey)){
623 foreach ($urlkey as $key) {
624 $hint .= ' AND ' . $key;
625 }
626 }
627 $urlkey = (!empty($filter['url_q_not']))? array_filter( array_map("trim", explode(PHP_EOL, $filter['url_q_not']))) : array();
628 if(!empty($urlkey)){
629 foreach ($urlkey as $key) {
630 $hint .= ' NOT ' . $key;
631 }
632 }
633 $hint .= PHP_EOL . PHP_EOL;
634 }
635 echo "<th class='filter-type urlfilter'><span title='$hint' style='font-size:smaller'>{$v}</span></th>";
636 }
637 } ?>
638 <th class="filter-type filter-none"><span style="font-size:smaller"><?php _e('Normal', 'plf'); ?></span></th>
639 <th class="filter-type filter-admin"><span style="font-size:smaller"><?php _e('Admin Type', 'plf'); ?></span></th>
640 <th class="filter-type filter-tmpl"><span style="font-size:smaller"><?php _e('Page Type', 'plf'); ?></span></th>
641 </tr>
642 </thead>
643 <tbody class="plugins-table-body">
644 <?php
645 //plugins filter registoration table
646 $plist = array();
647 foreach ( $plugins as $p_key => $val ) {
648 $name = self::pluginkey_to_name($p_key);
649 if(!empty($name))
650 $plist[$p_key] = '';
651 }
652 $jlist = $clist = array();
653 foreach ( $plist as $p_key => $val ) {
654 if(strpos($p_key, 'jetpack_module/') !== false){
655 $jlist[$p_key] = $plist[$p_key];
656 unset($plist[$p_key]);
657 }
658 else if(strpos($p_key, 'celtispack_module/') !== false){
659 $clist[$p_key] = $plist[$p_key];
660 unset($plist[$p_key]);
661 }
662 }
663 //Addon URL filter group key
664 $chklist = array();
665 if($urlnum > 0) {
666 foreach ( $groupkeys as $key) {
667 $chklist[] = $key;
668 }
669 }
670
671 foreach ( $plist as $p_key => $val ) {
672 $modules = array();
673 if(strpos($p_key, 'jetpack/') !== false)
674 $modules = $jlist;
675 else if(strpos($p_key, 'celtispack/') !== false)
676 $modules = $clist;
677 else
678 $this->plfregist_item($p_key, $chklist, $filter);
679 if(!empty($modules)){
680 echo "<input type='hidden' name='plfregist[$p_key]' value='_pagefilter'>";
681 foreach ( $modules as $m_key => $val) {
682 $this->plfregist_item($m_key, $chklist, $filter);
683 }
684 }
685 }
686 ?>
687 </tbody>
688 </table>
689 </div>
690 <p></p>
691 <div class="grid-row">
692 <div class="filter-description">
693 <p><strong>[ <?php _e('Page Type Filter', 'plf'); ?> ]</strong></p>
694 <?php _e('<strong>Normal</strong> - Exclude plugin from Page Type filter', 'plf'); ?><br />
695 <?php _e('<strong>Admin Type</strong> - If you only use plugins for Admin pages.', 'plf'); ?><br />
696 <?php _e('<strong>Page Type</strong> - If you want to activate or deactivate plugins for each Page Type and Single page.', 'plf'); ?>
697 <p>
698 <?php
699 $checked = (!empty(self::$filter['admin_bar']))? self::$filter['admin_bar'] : false;
700 echo '<span class="admin-bar-option">' . self::checkbox("plf_option[admin_bar]", $checked, __('Add a link to admin bar for displaying the plugins filtered status', 'plf') ) . '</span>';
701 ?>
702 </p>
703 <p><?php _e('* Plugins with `Page Type Filter` selected are blocked, but you can Activate it for various Page type in the `Page Type Activation` and Single Page setting.', 'plf'); ?></p>
704
705 <div class="exclude-pformat">
706 <p><?php _e('<strong>Exclude Post Format Type</strong> - Choose Post Format Type you are not using. To exclude from Page Type item subject.', 'plf'); ?></p>
707 <?php
708 $html = '<div>';
709 $pformat = array('image', 'gallery', 'video', 'audio', 'aside', 'status', 'quote', 'link', 'chat' );
710 foreach ( $pformat as $type ) {
711 $checked = (!empty(self::$filter['exclude'][$type]))? self::$filter['exclude'][$type] : false;
712 $label = "<span>$type</span>";
713 $html .= self::checkbox("plf_option[exclude][$type]", $checked, $label);
714 }
715 $html .= '</div>';
716 echo $html;
717 ?>
718 </div>
719 <p><strong>[ <?php _e('Post Language Locale', 'plf'); ?> ]</strong></p>
720 <p><?php _e('A very simple multilingual feature that uses MO translation files for the selected locale per Post/Page editing screen.','plf'); ?></p>
721 <?php
722 $checked = (!empty(self::$filter['language']))? self::$filter['language'] : false;
723 echo '<div class="language-option">' . self::checkbox("plf_option[language]", $checked, __('Language switching per post', 'plf') ) . '</div>';
724 ?>
725 </div>
726 <div class="side-info">
727 <?php if(! is_plugin_active('plugin-load-filter-addon/plugin-load-filter-addon.php')){ ?>
728 <div style="background-color: #f0fff0; border:1px solid #70c370; padding:4px 20px; margin: 10px 0;" >
729 <p><strong><?php _e('Introduction of Addon', 'plf'); ?></strong></p>
730 <p><?php _e('Thank you for using Plugin Load Filter. We offer URL filtering as Addon. Please consider using Addon!', 'plf'); ?></p>
731 <p><?php _e('See more information ', 'plf'); ?><a target="_blank" rel="noopener" href="https://celtislab.net/en/wp-plugin-load-filter-addon/"> Plugin Load Filter Addon</a></p>
732 </div>
733 <?php } ?>
734 <?php if(! is_plugin_active('yasakani-cache/yasakani-cache.php')){ ?>
735 <div style="background-color: #f0fff0; border:1px solid #70c370; padding:4px 20px; margin: 10px 0;" >
736 <p><strong><?php _e('Introduction of YASAKANI Cache', 'plf'); ?></strong></p>
737 <p><?php _e('We offer Free Cache Plugin that is very easy to use and very fast.', 'plf'); ?></p>
738 <p><?php _e('See more information ', 'plf'); ?><a target="_blank" rel="noopener" href="https://celtislab.net/en/wp-yasakani-cache/"> YASAKANI Cache</a></p>
739 <p><?php _e('We also sell Addon for Image Optimization, so please consider using them!', 'plf'); ?></p>
740 <p><?php _e('See more information ', 'plf'); ?><a target="_blank" rel="noopener" href="https://celtislab.net/en/wp-yasakani-cache-img-optimizer-addon/"> YASAKANI Image Optimizer</a></p>
741 </div>
742 <?php } ?>
743 </div>
744 </div>
745 <?php
746 }
747
748 //Activate plugins select from Page Filter
749 public function _plfactive_checkbox_row($p_key, $select_cvplugins, $chklist, $filter) {
750
751 $selplugins = array_map("trim", explode(',', $select_cvplugins));
752 $devlist = array('desktop','mobile');
753 if(in_array( $p_key, $selplugins )){
754 $p_name = self::pluginkey_to_name($p_key);
755 echo "<tr><td class='plugins-name'>$p_name</td>";
756 foreach($devlist as $devtype){
757 $checked = (empty($filter['group'][$devtype]['plugins']) || false === strpos($filter['group'][$devtype]['plugins'], $p_key))? false : true;
758 echo '<td class="device-type">' . self::altcheckbox("plfactive[$devtype][$p_key]", $checked, '<span class="dashicons dashicons-yes"></span>') . '</td>';
759 }
760 foreach($chklist as $pgtype){
761 $checked = (empty($filter['group'][$pgtype]['plugins']) || false === strpos($filter['group'][$pgtype]['plugins'], $p_key))? false : true;
762 echo '<td class="ckbox-type">' . self::altcheckbox("plfactive[$pgtype][$p_key]", $checked, '<span class="dashicons dashicons-admin-plugins"></span>') . '</td>';
763 }
764 echo "</tr>";
765 }
766 }
767
768 public function plfactive_table($plugins, $select_cvplugins, $filter) {
769 if(empty($select_cvplugins))
770 return;
771
772 ?>
773 <div id="wrap_activation-table">
774 <table id="activation-table" class="widefat">
775 <thead>
776 <tr><th class="plugins-name"><?php _e('Plugins'); ?></th>
777 <th class="device-type"><span title="<?php _e('Desktop Device', 'plf'); ?>" class="dashicons dashicons-desktop"></span><br /><span style="font-size:xx-small">Desktop</span></th>
778 <th class="device-type"><span title="<?php _e('Mobile Device', 'plf'); ?>" class="dashicons dashicons-smartphone"></span><br /><span style="font-size:xx-small">Mobile</span></th>
779 <th class="ckbox-type"><span title="<?php _e('Home/Front-page', 'plf'); ?>" class="dashicons dashicons-admin-home"></span><br /><span style="font-size:xx-small">Home</span></th>
780 <th class="ckbox-type"><span title="<?php _e('Archive page', 'plf'); ?>" class="dashicons dashicons-list-view"></span><br /><span style="font-size:xx-small">Archive</span></th>
781 <th class="ckbox-type"><span title="<?php _e('Search page', 'plf'); ?>" class="dashicons dashicons-search"></span><br /><span style="font-size:xx-small">Search</span></th>
782 <th class="ckbox-type"><span title="<?php _e('Attachment page', 'plf'); ?>" class="dashicons dashicons-media-default"></span><br /><span style="font-size:xx-small">Attach</span></th>
783 <th class="ckbox-type"><span title="<?php _e('Page', 'plf'); ?>" class="dashicons dashicons-admin-page"></span><br /><span style="font-size:xx-small">Page</span></th>
784 <th class="ckbox-type pformat"><span title="<?php _e('Post : ', 'plf'); _e('Standard', 'plf'); ?>" class="dashicons dashicons-admin-post"></span><br /><span style="font-size:xx-small">Post</span></th>
785 <?php
786 $pformat = array('image', 'gallery', 'video', 'audio', 'aside', 'status', 'quote', 'link', 'chat' );
787 $exclude = array();
788 if(!empty($filter['exclude'])){
789 foreach ( $filter['exclude'] as $type => $v) {
790 if(!empty($v)){
791 $exclude[] = $type;
792 }
793 }
794 }
795 foreach ( $pformat as $type) {
796 if(!in_array($type, $exclude)){
797 $title = __('Post : ', 'plf') . $type;
798 $icon = ($type === "link")? "dashicons-admin-links" : "dashicons-format-$type";
799 echo '<th class="ckbox-type pformat"><span title="' . $title . '" class="dashicons ' . $icon .'"></span><br /><span style="font-size:xx-small">' . $type .'</span></th>';
800 }
801 }
802 if(function_exists('is_embed')){
803 $title = __('WordPress Embed Content Card (API)', 'plf');
804 echo "<th class='ckbox-type tmpl-embed'><span title='$title' style='font-size:xx-small'>Embed Content</span></th>";
805 }
806 $post_types = get_post_types( array('public' => true, '_builtin' => false) );
807 foreach ( $post_types as $post_type ) {
808 if(!empty($post_type)){
809 $title = __('Custom Post : ', 'plf') . $post_type;
810 echo "<th class='ckbox-type tmpl-custom'><span title='$title' style='font-size:xx-small'>$post_type</span></th>";
811 }
812 }
813 ?>
814 </tr>
815 </thead>
816 <tbody class="plugins-table-body">
817 <?php
818 $nplugins = $jmodules = $cmodules = $allmodule = array();
819 foreach ( $plugins as $p_key => $p_data ) {
820 $p_name = self::pluginkey_to_name($p_key);
821 if(empty($p_name))
822 continue;
823 if(strpos($p_key, 'jetpack_module/') !== false)
824 $jmodules[] = $p_key;
825 else if(strpos($p_key, 'celtispack_module/') !== false)
826 $cmodules[] = $p_key;
827 else
828 $nplugins[] = $p_key;
829 }
830 $chklist = array('home', 'archive', 'search', 'attachment', 'page', 'post');
831 foreach ( $pformat as $type) {
832 if(!in_array($type, $exclude)){
833 $chklist[] = "post-$type";
834 }
835 }
836 if(function_exists('is_embed')){
837 $chklist[] = 'content-card';
838 }
839 $post_types = get_post_types( array('public' => true, '_builtin' => false) );
840 foreach ( $post_types as $post_type ) {
841 $chklist[] = $post_type;
842 }
843 foreach ( $nplugins as $p_key ) {
844 if(strpos($p_key, 'jetpack/') !== false){
845 foreach ( $jmodules as $p_key ) {
846 $this->_plfactive_checkbox_row($p_key, $select_cvplugins, $chklist, $filter);
847 }
848 }
849 else if(strpos($p_key, 'celtispack/') !== false){
850 foreach ( $cmodules as $p_key ) {
851 $this->_plfactive_checkbox_row($p_key, $select_cvplugins, $chklist, $filter);
852 }
853 }
854 else {
855 $this->_plfactive_checkbox_row($p_key, $select_cvplugins, $chklist, $filter);
856 }
857 }
858 ?>
859 </tbody>
860 </table>
861 </div>
862 <?php
863 }
864
865 //Option Setting Form Display
866 public function plf_option_page() {
867 $clear_dialog = __('Plugin Load Filter Settings\nClick OK to clear it.', 'plf');
868 ?>
869 <h2><?php _e('Plugin Load Filter Settings', 'plf'); ?></h2>
870 <p></p>
871 <div id="plf-setting-tabs">
872 <ul>
873 <li><a href="#plf-registration-tab" ><?php _e('Filter Registration', 'plf'); ?></a></li>
874 <li><a href="#plf-activation-tab" ><?php _e('Page Type Activation', 'plf'); ?></a></li>
875 </ul>
876 <div id="plf-registration-tab" style="display : none;">
877 <form method="post" autocomplete="off">
878 <?php wp_nonce_field( 'plugin_load_filter'); ?>
879 <?php $this->plfregist_table(self::$plugins_inf, self::$filter); ?>
880 <p class="submit">
881 <input type="submit" class="button-primary" name="clear_regist_filter" value="<?php _e('Clear', 'plf'); ?>" onclick="return confirm('<?php echo $clear_dialog; ?>')" />&nbsp;&nbsp;&nbsp;
882 <input type="submit" class="button-primary" name="edit_regist_filter" value="<?php _e('Filter Entry &raquo;', 'plf'); ?>" />
883 </p>
884 </form>
885 </div>
886 <div id="plf-activation-tab" style="display : none;">
887 <form method="post" autocomplete="off">
888 <?php wp_nonce_field( 'plugin_load_filter'); ?>
889 <?php
890 $pgfilter = (!empty(self::$filter['_pagefilter']['plugins']))? self::$filter['_pagefilter']['plugins'] : array();
891 if(!empty($pgfilter)){
892 $this->plfactive_table(self::$plugins_inf, $pgfilter, self::$filter);
893 ?>
894 <br />
895 <p><?php _e('Select plugins to be activated for each page type by clicking on <span class="dashicons dashicons-admin-plugins"></span> mark from "page type filter" registered plugins.', 'plf') ?><br />
896 <?php _e('You can also select plugins to activate from Post/Page content editing screen.', 'plf') ?>
897 </p>
898 <p class="submit">
899 <input type="submit" class="button-primary" name="clear_activate_page_filter" value="<?php _e('Clear', 'plf'); ?>" onclick="return confirm('<?php echo $clear_dialog; ?>')" />&nbsp;&nbsp;&nbsp;
900 <input type="submit" class="button-primary" name="edit_activate_page_filter" value="<?php _e('Activate Plugin Entry &raquo;', 'plf'); ?>" />
901 </p>
902 <?php
903 } else {
904 ?>
905 <br />
906 <p><span style="color: #ff0000;"><?php _e('Page Filter is not registered', 'plf') ?></span></p>
907 <?php
908 }
909 ?>
910 </form>
911 </div>
912 </div>
913 <?php
914 }
915
916 /***************************************************************************
917 * Meta box
918 * Individual of the plug-in filter meta box for Post/Page/CustomPost
919 **************************************************************************/
920 function load_meta_boxes( $post_type, $post ) {
921 if ( current_user_can('activate_plugins', $post->ID) ) {
922 add_meta_box( 'pluginfilterdiv', __( 'Plugin Load Filter', 'plf' ), array(&$this, 'plf_meta_box'), null, 'side' );
923 //add_action( 'admin_head', array(&$this, 'plf_css' ));
924 add_action( 'admin_footer', array(&$this, 'plf_meta_script' ));
925 }
926 }
927
928 //Plugin pagefilter Selected Checkbox (plugin and modules list)
929 // $p_key
930 // $select_cvplugins : csv string : pagefilter selected plugins
931 // $checked_arcvplugins : array csv string : ['desktop']=desktop enable plugins, ['mobile']=mobile enable plugins
932 function _pagefilter_plugins_checklist( $p_key, $select_cvplugins, $checked_arcvplugins ) {
933
934 $html = '';
935 $selplugins = array_map("trim", explode(',', $select_cvplugins));
936 $device['desktop'] = $checked_arcvplugins['desktop'];
937 $device['mobile'] = $checked_arcvplugins['mobile'];
938 $devlist = array('desktop','mobile');
939 if(in_array( $p_key, $selplugins )){
940 $p_name = self::pluginkey_to_name($p_key);
941 $html .= "<tr><td class='plugins-name'>$p_name</td>";
942 foreach($devlist as $devtype){
943 $checked = (empty($device[$devtype]) || false === strpos($device[$devtype], $p_key))? false : true;
944 $html .= "<td class='device-type $devtype'>" . self::altcheckbox("plf_option[$devtype][$p_key]", $checked, '<span class="dashicons dashicons-yes"></span>') . '</td>';
945 }
946 $html .= "</tr>";
947 }
948 return $html;
949 }
950
951 //Plugin pagefilter Selected Checkbox (plugin and modules list)
952 // $plugins : array : all active plugins
953 // $select_cvplugins : csv string : pagefilter selected plugins
954 // $checked_arcvplugins : array csv string : ['desktop']=desktop enable plugins, ['mobile']=mobile enable plugins
955 function pagefilter_plugins_checklist( $plugins, $select_cvplugins, $checked_arcvplugins ) {
956
957 if(empty($select_cvplugins))
958 return __('Page Filter is not registered', 'plf');
959
960 $html = '<table id="activation-table">';
961 $html .= '<thead>';
962 $html .= '<tr><th class="plugins-name">'. __('Plugins') . '</th>';
963 $html .= '<th class="device-type"><span title="'. __('Desktop Device', 'plf'). '" class="dashicons dashicons-desktop"></span><br /><span style="font-size:xx-small">Desktop</span></th>';
964 $html .= '<th class="device-type"><span title="'. __('Mobile Device', 'plf'). '" class="dashicons dashicons-smartphone"></span><br /><span style="font-size:xx-small">Mobile</span></th>';
965 $html .= '</tr>';
966 $html .= '</thead>';
967 $html .= '<tbody class="plugins-table-body meta-boxes-plugins-table">';
968 $nplugins = $jmodules = $cmodules = array();
969 foreach ( $plugins as $p_key => $p_data ) {
970 $p_name = self::pluginkey_to_name($p_key);
971 if(empty($p_name))
972 continue;
973 if(strpos($p_key, 'jetpack_module/') !== false)
974 $jmodules[] = $p_key;
975 else if(strpos($p_key, 'celtispack_module/') !== false)
976 $cmodules[] = $p_key;
977 else
978 $nplugins[] = $p_key;
979 }
980
981 foreach ( $nplugins as $p_key ) {
982 if(strpos($p_key, 'jetpack/') !== false){
983 foreach ( $jmodules as $p_key ) {
984 $html .= $this->_pagefilter_plugins_checklist( $p_key, $select_cvplugins, $checked_arcvplugins );
985 }
986 }
987 else if(strpos($p_key, 'celtispack/') !== false){
988 foreach ( $cmodules as $p_key ) {
989 $html .= $this->_pagefilter_plugins_checklist( $p_key, $select_cvplugins, $checked_arcvplugins );
990 }
991 }
992 else {
993 $html .= $this->_pagefilter_plugins_checklist( $p_key, $select_cvplugins, $checked_arcvplugins );
994 }
995 }
996 $html .= '</tbody>';
997 $html .= '</table>';
998 return $html;
999 }
1000
1001 //get_locale に加えたフィルターフックを外してサイトのロケールを取得
1002 static function get_site_locale(){
1003 global $locale;
1004 if(class_exists('Plf_filter')){
1005 remove_filter( 'locale', array('Plf_filter', 'post_locale') );
1006 $locale = null;
1007 $site_locale = get_locale();
1008 add_filter( 'locale', array('Plf_filter', 'post_locale') );
1009 } else {
1010 $locale = null;
1011 $site_locale = get_locale();
1012 }
1013 return $site_locale;
1014 }
1015
1016 function plf_meta_box( $post, $box ) {
1017 if(is_object($post)){
1018 $myfilter = get_post_meta( $post->ID, '_plugin_load_filter', true );
1019 //ver2.2.0 for compatibility, set 'plugins' data to 'desktop' and 'mobile'
1020 if(!empty($myfilter['plugins'])){
1021 $myfilter['desktop'] = $myfilter['plugins'];
1022 $myfilter['mobile'] = $myfilter['plugins'];
1023 unset($myfilter['plugins']);
1024 }
1025 $default = array( 'filter' => 'default', 'desktop' => '', 'mobile' => '');
1026 $option = (!empty($myfilter))? $myfilter : $default;
1027 $option = wp_parse_args( $option, $default);
1028 $pgfilter = (!empty(self::$filter['_pagefilter']['plugins']))? self::$filter['_pagefilter']['plugins'] : array();
1029 $ajax_nonce = wp_create_nonce( 'plugin_load_filter-' . $post->ID );
1030 $this->plf_css();
1031 ?>
1032 <div id="plugin-filter-select">
1033 <p><?php _e( 'Plugin filter for Single post', 'plf' ); ?></p>
1034 <label><input type="radio" name="pagefilter" value="default" <?php checked('default', $option['filter']); ?>/><?php _e('Not Use', 'plf' ); ?></label>
1035 <label><input type="radio" name="pagefilter" value="include" <?php checked('include', $option['filter']); ?>/><?php _e('Use', 'plf'); ?></label>
1036 <div id="page-filter-stat">
1037 <?php echo $this->pagefilter_plugins_checklist( self::$plugins_inf, $pgfilter, $option ); ?>
1038 </div>
1039 <div class="plf-option-info"><?php _e('Plugin Activate/Deactivate filter for this Post only', 'plf'); ?></div>
1040 <?php
1041 $c_locale = $o_post_id = '';
1042 $languages = array();
1043 $locale_mode = 'style="display:none;"';
1044 if(!empty(self::$filter['language'])){
1045 $languages = get_available_languages();
1046 $site_locale = self::get_site_locale();
1047 //bogo と同じ _locale ポストメタデータ採用
1048 $c_locale = get_post_meta( $post->ID, '_locale', true );
1049 if(empty($c_locale)){
1050 $c_locale = $site_locale;
1051 }
1052 if ( ! in_array( $c_locale, $languages ) ) {
1053 $c_locale = '';
1054 }
1055 $o_post_id = get_post_meta( $post->ID, '_original_post_id', true );
1056 $o_post_id = (is_numeric($o_post_id))? $o_post_id : '';
1057 $locale_mode = '';
1058 }
1059 echo '<p class="hide-if-no-js"><a id="plugin-filter-submit" class="button" href="#pluginfilterdiv" onclick="WPAddPagePluginLoadFilter(\'' . $ajax_nonce . '\');return false;" >'. __('Save') .'</a></p>';
1060 ?>
1061 <hr>
1062 <div id="plf-post-locale-select" <?php echo $locale_mode; ?>>
1063 <p><?php _e( 'Language of this post', 'plf' ); ?> <span class="dashicons dashicons-translation" aria-hidden="true"></span></p>
1064 <?php
1065 wp_dropdown_languages(
1066 array(
1067 'name' => 'plf_locale',
1068 'id' => 'plf_locale',
1069 'selected' => $c_locale,
1070 'languages' => $languages,
1071 'show_available_translations' => false,
1072 )
1073 );
1074 ?>
1075 <p><?php _e( 'Original post ID for hreflang', 'plf' ); ?></p>
1076 <input type="text" id="plf_original_post_id" name="plf_original_post_id" size="8" value="<?php echo $o_post_id; ?>" />
1077 <div class="hflang-group-edit-lonk">
1078 <?php
1079 $id = (is_numeric($o_post_id))? $o_post_id : $post->ID;
1080 global $wpdb;
1081 $ids = $wpdb->get_row($wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_original_post_id' AND meta_value = %d", $id ), ARRAY_N);
1082 if(is_array($ids)){
1083 if(!in_array((string)$id, $ids)){
1084 $ids[] = (string)$id;
1085 }
1086 if(count($ids) > 1){
1087 foreach ($ids as $v) {
1088 if( $post->ID != $v){
1089 $l = get_post_meta( $v, '_locale', true );
1090 if(empty($l)){
1091 $l = $site_locale;
1092 }
1093 $url = get_edit_post_link( $v );
1094 if(!empty($url)){
1095 echo '<p><a class="edit-post-locale-link" target="_blank" href="' . $url .'" rel="external noreferrer noopener">' . __('Edit Post') . " ($l) " . '<span class="dashicons dashicons-external" aria-hidden="true"></span></a></p>';
1096 }
1097 }
1098 }
1099 }
1100 }
1101 ?>
1102 </div>
1103 <div class="plf-option-info"><?php _e('Use MO translation file for the selected locale. When Original Post ID is registered, posts with the same Original Post ID are treated as `hreflang` metadata group.', 'plf'); ?></div>
1104 <?php echo '<p class="hide-if-no-js"><a id="plugin-filter-submit" class="button" href="#pluginfilterdiv" onclick="WPAddPagePluginLoadFilter(\'' . $ajax_nonce . '\');return false;" >'. __('Save') .'</a></p>'; ?>
1105 </div>
1106 </div>
1107 <?php
1108 }
1109 }
1110
1111 //wp_ajax_plugin_load_filter called function
1112 function plf_ajax_postidfilter() {
1113 if ( isset($_POST['post_id']) ) {
1114 $pid = (int) $_POST['post_id'];
1115 if ( !current_user_can( 'activate_plugins', $pid ) )
1116 wp_die( -1 );
1117 check_ajax_referer( "plugin_load_filter-$pid" );
1118
1119 if(!empty(self::$filter['language'])){
1120 if(isset($_POST['locale'])){
1121 $s_locale = (!empty($_POST['locale']))? $_POST['locale'] : 'en_US';
1122 $languages = get_available_languages();
1123 $languages[] = 'en_US'; //add WP default locale
1124 if ( in_array( $s_locale, $languages ) ) {
1125 $g_locale = get_post_meta( $pid, '_locale', true );
1126 global $locale;
1127 if(class_exists('Plf_filter')){
1128 remove_filter( 'locale', array('Plf_filter', 'post_locale') );
1129 $locale = null;
1130 $o_locale = get_locale();
1131 add_filter( 'locale', array('Plf_filter', 'post_locale') );
1132 } else {
1133 $locale = null;
1134 $o_locale = get_locale();
1135 }
1136 if($s_locale !== $g_locale){
1137 if($s_locale === $o_locale){
1138 delete_post_meta( $pid, '_locale');
1139 } else {
1140 update_post_meta( $pid, '_locale', $s_locale );
1141 }
1142 }
1143 }
1144 }
1145 if(isset($_POST['original_post_id'])){
1146 if(empty($_POST['original_post_id'])){
1147 delete_post_meta( $pid, '_original_post_id');
1148 } elseif(preg_match( '/([0-9]+)?/', $_POST['original_post_id'], $match )){
1149 $o_id = (int)$match[1];
1150 $post = get_post($o_id);
1151 if(is_object($post)){
1152 $g_id = get_post_meta( $pid, '_original_post_id', true );
1153 if($o_id !== $g_id){
1154 update_post_meta( $pid, '_original_post_id', $o_id );
1155 }
1156 }
1157 }
1158 }
1159 }
1160 $pgfilter = (!empty(self::$filter['_pagefilter']['plugins']))? self::$filter['_pagefilter']['plugins'] : array();
1161 $option["filter"] = (empty($_POST['filter']))? 'default' : $_POST['filter'];
1162 if('default' == $option["filter"]){
1163 delete_post_meta( $pid, '_plugin_load_filter');
1164 } else {
1165 $plugins = array();
1166 if( preg_match_all('/plf_option\[desktop\]\[(.+?)\]/u', $_POST['desktop'], $matches)){
1167 if(!empty($matches[1])){
1168 foreach ($matches[1] as $plugin){
1169 $plugins[] = $plugin;
1170 }
1171 $option["desktop"] = implode(",", $plugins);
1172 }
1173 }
1174 $plugins = array();
1175 if( preg_match_all('/plf_option\[mobile\]\[(.+?)\]/u', $_POST['mobile'], $matches)){
1176 if(!empty($matches[1])){
1177 foreach ($matches[1] as $plugin){
1178 $plugins[] = $plugin;
1179 }
1180 $option["mobile"] = implode(",", $plugins);
1181 }
1182 }
1183 update_post_meta( $pid, '_plugin_load_filter', $option );
1184 }
1185
1186 $html = $this->pagefilter_plugins_checklist( self::$plugins_inf, $pgfilter, $option );
1187 wp_send_json_success($html);
1188 }
1189 wp_die( 0 );
1190 }
1191
1192 //alternate hreflang meta tag
1193 static function altenate_hreflang() {
1194 if ( is_singular() ) {
1195 global $post, $wpdb;
1196 $pid = $post->ID;
1197 $oid = get_post_meta( $pid, '_original_post_id', true );
1198 $oid = (is_numeric($oid))? $oid : $pid;
1199 $ids = $wpdb->get_row($wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_original_post_id' AND meta_value = %d", $oid ), ARRAY_N);
1200 if(is_array($ids)){
1201 if(!in_array((string)$oid, $ids)){
1202 $ids[] = (string)$oid;
1203 }
1204 if(count($ids) > 1){
1205 $site_locale = self::get_site_locale();
1206 foreach ($ids as $id) {
1207 $l = get_post_meta( $id, '_locale', true );
1208 if(empty($l)){
1209 $l = $site_locale;
1210 }
1211 $url = get_permalink( $id );
1212 echo '<link rel="alternate" hreflang="' . esc_attr($l) . '" href="' . esc_url($url) .'" />' . PHP_EOL;
1213 }
1214 }
1215 }
1216 }
1217 }
1218
1219 /***************************************************************************
1220 * Javascript
1221 **************************************************************************/
1222 function activetab_script() { ?>
1223 <script type='text/javascript' >
1224 /* <![CDATA[ */
1225 var plf_activetab = <?php echo $this->tab_num; ?>
1226 /* ]]> */
1227 jQuery(document).ready(function ($) {
1228 plf_setting_tabs();
1229 function plf_setting_tabs(){ $('#plf-setting-tabs').tabs({ active:plf_activetab, }); }
1230 });
1231 </script>
1232 <?php }
1233
1234 function plf_meta_script() {
1235 $reload_dialog = __('Plugin Load Filter setting has been updated.\nClick OK to reload the page.', 'plf');
1236 ?>
1237 <script type='text/javascript' >
1238 WPAddPagePluginLoadFilter = function(nonce){
1239 jQuery.ajax({
1240 type: 'POST',
1241 url: ajaxurl,
1242 data: {
1243 action: "plugin_load_filter",
1244 post_id : jQuery( '#post_ID' ).val(),
1245 _ajax_nonce: nonce,
1246 locale: jQuery("select[name='plf_locale']").val(),
1247 original_post_id: jQuery("input[name='plf_original_post_id']").val(),
1248 filter: jQuery("input[name='pagefilter']:checked").val(),
1249 desktop: jQuery('.meta-boxes-plugins-table td.desktop input:checked').map(function(){ return jQuery(this).attr("name"); }).get().join(','),
1250 mobile: jQuery('.meta-boxes-plugins-table td.mobile input:checked').map(function(){ return jQuery(this).attr("name"); }).get().join(','),
1251 },
1252 dataType: 'json',
1253 }).then(
1254 function (response, dataType) {
1255 jQuery('#page-filter-stat').html(response.data);
1256 if(window.confirm('<?php echo $reload_dialog; ?>')){
1257 location.reload();
1258 }
1259 },
1260 function () { /* alert("ajax error"); */ }
1261 );
1262 };
1263 </script>
1264 <?php }
1265 }