| 1 |
<?php |
| 2 |
|
| 3 |
namespace Gianism\Controller; |
| 4 |
|
| 5 |
use Gianism\Helper\ExtensionManager; |
| 6 |
use Gianism\Pattern\AbstractController; |
| 7 |
use Gianism\Service\Google; |
| 8 |
use Gianism\UI\Screen; |
| 9 |
|
| 10 |
|
| 11 |
/** |
| 12 |
* Create admin panel for Gianism |
| 13 |
* |
| 14 |
* @package Gianism |
| 15 |
* @author Takahashi Fumiki |
| 16 |
* @since 2.0.0 |
| 17 |
*/ |
| 18 |
class Admin extends AbstractController { |
| 19 |
|
| 20 |
use ExtensionManager; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $views = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* Invalid option |
| 29 |
*/ |
| 30 |
protected $invalid_options = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor executed on admin_menu hook |
| 34 |
* |
| 35 |
* @param array $argument |
| 36 |
*/ |
| 37 |
protected function __construct( array $argument = [] ) { |
| 38 |
parent::__construct( $argument ); |
| 39 |
if ( $this->option->is_network_activated() && ! is_main_site() ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
//Create plugin link |
| 43 |
add_filter( 'plugin_action_links', [ $this, 'plugin_page_link' ], 10, 2 ); |
| 44 |
add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 4 ); |
| 45 |
// Register script |
| 46 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 47 |
|
| 48 |
$admin_screens = [ |
| 49 |
'Gianism\\UI\\SettingScreen', |
| 50 |
]; |
| 51 |
|
| 52 |
/** |
| 53 |
* These classes will be rendered |
| 54 |
* |
| 55 |
* @filter gianism_admin_screens |
| 56 |
* @param array $admin_screens An array of class name. |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
$admin_screens = apply_filters( 'gianism_admin_screens', $admin_screens ); |
| 60 |
|
| 61 |
// Call all screen |
| 62 |
foreach ( $admin_screens as $class_name ) { |
| 63 |
if ( $this->is_callable( $class_name, 'Gianism\\UI\\Screen' ) ) { |
| 64 |
/** @var Screen $instance */ |
| 65 |
$instance = new $class_name(); |
| 66 |
} |
| 67 |
} |
| 68 |
// No service is available. |
| 69 |
if ( ! $this->option->is_enabled() ) { |
| 70 |
$this->invalid_options[] = sprintf( $this->_( 'No service is enabled. Please go to <a href="%s">Gianism Setting</a> and follow instructions there.' ), admin_url( 'options-general.php?page=gianism&view=setting' ) ); |
| 71 |
} |
| 72 |
// Check permalink |
| 73 |
if ( ! $this->option->get( 'rewrite_rules', '' ) ) { |
| 74 |
$this->invalid_options[] = sprintf( $this->_( 'You should set rewrite rules. Go to <a href="%s">Permalink Setting</a> and enable it.' ), admin_url( 'options-permalink.php' ) ); |
| 75 |
} |
| 76 |
// Check old setting |
| 77 |
if ( current_user_can( 'manage_options' ) && $this->option->has_invalid_option( 'google_redirect' ) ) { |
| 78 |
$this->invalid_options[] = sprintf( $this->_( 'Google redirect URL is deprecated since version 2.0. <strong>You must change setting on Google API Console</strong>. Please <a href="%s">update option</a> and follow the instruction there.' ), admin_url( 'options-general.php?page=gianism' ) ); |
| 79 |
} |
| 80 |
add_action( 'admin_notices', [ $this, 'invalid_option_notices' ] ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Register assets |
| 85 |
* |
| 86 |
* @param string $hook_suffix |
| 87 |
*/ |
| 88 |
public function admin_enqueue_scripts( $hook_suffix ) { |
| 89 |
// Setting page and profile page |
| 90 |
wp_enqueue_script( $this->name . '-admin-helper' ); |
| 91 |
// Other |
| 92 |
wp_enqueue_style( $this->name . '-admin-panel' ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Detect shouldn't display notices. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function no_nag_notice() { |
| 101 |
return defined( 'DISABLE_NAG_NOTICES' ) && DISABLE_NAG_NOTICES; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Show message is options are invalid. |
| 106 |
*/ |
| 107 |
public function invalid_option_notices() { |
| 108 |
if ( $this->no_nag_notice() || empty( $this->invalid_options ) ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
array_unshift( $this->invalid_options, '<strong>[Gianism]</strong>' ); |
| 112 |
printf( '<div class="error"><p>%s</p></div>', wp_kses_post( implode( '<br />', $this->invalid_options ) ) ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Setup plugin links. |
| 117 |
* |
| 118 |
* @param array $links |
| 119 |
* @param string $file |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function plugin_page_link( $links, $file ) { |
| 124 |
if ( false !== strpos( $file, 'wp-gianism' ) ) { |
| 125 |
foreach ( [ |
| 126 |
admin_url( 'options-general.php?page=gianism' ) => $this->_( 'Settings' ), |
| 127 |
] as $url => $label ) { |
| 128 |
array_unshift( $links, sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html( $label ) ) ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $links; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
/** |
| 137 |
* Plugin row meta |
| 138 |
* |
| 139 |
* @param array $plugin_meta |
| 140 |
* @param string $plugin_file |
| 141 |
* @param array $plugin_data |
| 142 |
* @param string $status |
| 143 |
* |
| 144 |
* @return mixed |
| 145 |
*/ |
| 146 |
public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) { |
| 147 |
if ( false !== strpos( $plugin_file, 'wp-gianism' ) ) { |
| 148 |
foreach ( $plugin_meta as $index => $value ) { |
| 149 |
if ( preg_match( '#href="https://gianism.info"#', $value ) ) { |
| 150 |
$plugin_meta[ $index ] = preg_replace_callback( |
| 151 |
'#href="https://gianism.info"#', |
| 152 |
function ( $matches ) { |
| 153 |
return sprintf( |
| 154 |
'href="%s"', |
| 155 |
esc_url( |
| 156 |
gianism_utm_link( |
| 157 |
'https://gianism.info/', |
| 158 |
[ |
| 159 |
'utm_medium' => 'plugin_list_author', |
| 160 |
] |
| 161 |
) |
| 162 |
) |
| 163 |
); |
| 164 |
}, |
| 165 |
$value |
| 166 |
); |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
$plugin_meta[] = sprintf( |
| 171 |
'<a href="%s">%s</a>', |
| 172 |
esc_url( |
| 173 |
gianism_utm_link( |
| 174 |
'https://gianism.info/', |
| 175 |
[ |
| 176 |
'utm_medium' => 'plugin_list_support', |
| 177 |
] |
| 178 |
) |
| 179 |
), |
| 180 |
$this->_( 'Support' ) |
| 181 |
); |
| 182 |
$plugin_meta[] = '<a href="https://github.com/fumikito/Gianism">Github</a>'; |
| 183 |
} |
| 184 |
|
| 185 |
return $plugin_meta; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Detect if this is Gianism admin settings. |
| 190 |
* |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
public function is_gianism_admin() { |
| 194 |
return is_admin() && ( 'gianism' === filter_input( INPUT_GET, 'page' ) ); |
| 195 |
} |
| 196 |
} |
| 197 |
|