PluginProbe
Plugin Load Filter / 3.3.0
Plugin Load Filter v3.3.0
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 3.3.0, at plugin-load-filter.php

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