| 1 |
<?php |
| 2 |
|
| 3 |
namespace Gianism; |
| 4 |
|
| 5 |
|
| 6 |
/** |
| 7 |
* Create admin panel for Gianism |
| 8 |
* |
| 9 |
* @package Gianism |
| 10 |
* @author Takahashi Fumiki |
| 11 |
* @since 2.0.0 |
| 12 |
*/ |
| 13 |
class Admin extends Pattern\Singleton |
| 14 |
{ |
| 15 |
|
| 16 |
/** |
| 17 |
* Constructor |
| 18 |
* |
| 19 |
* @param array $argument |
| 20 |
*/ |
| 21 |
protected function __construct( array $argument = array() ){ |
| 22 |
// Add admin page |
| 23 |
add_options_page($this->_('Gianism Setting'), $this->_("Gianism Setting"), 'manage_options', 'gianism', array($this, 'render')); |
| 24 |
//Create plugin link |
| 25 |
add_filter('plugin_action_links', array($this, 'plugin_page_link'), 10, 2); |
| 26 |
add_filter('plugin_row_meta', array($this, 'plugin_row_meta'), 10, 4); |
| 27 |
// Add option save hook |
| 28 |
add_action( 'load-settings_page_gianism', array($this, 'update_option')); |
| 29 |
// Register script |
| 30 |
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 31 |
// Notice |
| 32 |
add_action('admin_notices', array($this, 'invalid_option_notices')); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Render admin panel |
| 37 |
*/ |
| 38 |
public function render(){ |
| 39 |
$this->get_template('general'); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get admin panel URL |
| 44 |
* |
| 45 |
* @param string $view 'setup', 'customize', 'advanced' |
| 46 |
* @return string|void |
| 47 |
*/ |
| 48 |
public function setting_url($view = ''){ |
| 49 |
$query = array( |
| 50 |
'page' => 'gianism', |
| 51 |
); |
| 52 |
if( $view ){ |
| 53 |
$query['view'] = $view; |
| 54 |
} |
| 55 |
return admin_url('options-general.php?'.http_build_query($query)); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Detect current admin panel |
| 60 |
* |
| 61 |
* @param string $view |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
protected function is_view($view = ''){ |
| 65 |
if( 'gianism' != $this->request('page') || 'options-general.php' !== basename($_SERVER['SCRIPT_FILENAME']) ){ |
| 66 |
return false; |
| 67 |
} |
| 68 |
$requested_view = $this->request('view'); |
| 69 |
switch($view){ |
| 70 |
case 'setup': |
| 71 |
case 'customize': |
| 72 |
case 'advanced': |
| 73 |
return $view == $requested_view; |
| 74 |
break; |
| 75 |
case 'setting': |
| 76 |
default: |
| 77 |
return (empty($requested_view) || 'setting' == $requested_view ); |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Load template file |
| 84 |
* |
| 85 |
* @param string $name |
| 86 |
*/ |
| 87 |
private function get_template($name){ |
| 88 |
$path = $this->dir.'templates'.DIRECTORY_SEPARATOR."{$name}.php"; |
| 89 |
if( file_exists($path) ){ |
| 90 |
$option = Option::get_instance(); |
| 91 |
include $path; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Register assets |
| 97 |
* |
| 98 |
* @param string $hook_suffix |
| 99 |
*/ |
| 100 |
public function admin_enqueue_scripts($hook_suffix){ |
| 101 |
// Only on setting page |
| 102 |
if( 'settings_page_gianism' == $hook_suffix ){ |
| 103 |
if( !is_null($this->request('view')) ){ |
| 104 |
wp_enqueue_style('gianism-syntax-highlighter-core', $this->url.'assets/syntax-highlighter/shCore.css', null, '3.0.83'); |
| 105 |
wp_enqueue_style('gianism-syntax-highlighter-default', $this->url.'assets/syntax-highlighter/shThemeDefault.css', null, '3.0.83'); |
| 106 |
wp_enqueue_script('gianism-syntax-highlighter-core', $this->url.'assets/syntax-highlighter/shCore.js', null, '3.0.83'); |
| 107 |
wp_enqueue_script('gianism-syntax-highlighter-php', $this->url.'assets/syntax-highlighter/shBrushPhp.js', null, '3.0.83'); |
| 108 |
} |
| 109 |
wp_enqueue_script($this->name.'-admin-helper', $this->url.'assets/compass/js/admin-helper.js', array('jquery'), $this->version, true); |
| 110 |
} |
| 111 |
// Setting page and profile page |
| 112 |
if( false !== array_search($hook_suffix, array('settings_page_gianism', 'profile.php'))){ |
| 113 |
wp_enqueue_style($this->name.'-admin-panel', $this->url.'assets/compass/stylesheets/gianism-admin.css', array('ligature-symbols'), $this->version); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Update option |
| 119 |
*/ |
| 120 |
public function update_option(){ |
| 121 |
if( $this->verify_nonce('option') ){ |
| 122 |
/** @var \Gianism\Option $option */ |
| 123 |
$option = Option::get_instance(); |
| 124 |
$option->update(); |
| 125 |
wp_redirect($this->setting_url()); |
| 126 |
exit; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Show message is options are invalid. |
| 132 |
*/ |
| 133 |
public function invalid_option_notices(){ |
| 134 |
$message = array(); |
| 135 |
/** @var \Gianism\Option $option */ |
| 136 |
$option = Option::get_instance(); |
| 137 |
if( current_user_can('manage_options') && $option->has_invalid_option('google_redirect')){ |
| 138 |
$message[] = 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.'), $this->setting_url()); |
| 139 |
} |
| 140 |
if( !$this->is_enabled() ){ |
| 141 |
$message[] = sprintf($this->_('No service is enabled. Please go to <a href="%s">Gianism Setiting</a> and follow instructions there.'), $this->setting_url()); |
| 142 |
} |
| 143 |
if( !empty($message) ){ |
| 144 |
array_unshift($message, '<strong>[Gianism]</strong>'); |
| 145 |
printf('<div class="error"><p>%s</p></div>', implode('<br />', $message)); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Setup plugin links. |
| 151 |
* |
| 152 |
* @param array $links |
| 153 |
* @param string $file |
| 154 |
* @return array |
| 155 |
*/ |
| 156 |
public function plugin_page_link($links, $file){ |
| 157 |
if(false !== strpos($file, 'wp-gianism')){ |
| 158 |
array_unshift($links, '<a href="'.$this->setting_url().'">'.__('Settings').'</a>'); |
| 159 |
} |
| 160 |
return $links; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Plugin row meta |
| 166 |
* |
| 167 |
* @param array $plugin_meta |
| 168 |
* @param string $plugin_file |
| 169 |
* @param array $plugin_data |
| 170 |
* @param string $status |
| 171 |
* @return mixed |
| 172 |
*/ |
| 173 |
public function plugin_row_meta($plugin_meta, $plugin_file, $plugin_data, $status){ |
| 174 |
if(false !== strpos($plugin_file, 'wp-gianism')){ |
| 175 |
for($i = 0, $l = count($plugin_meta); $i < $l; $i++ ){ |
| 176 |
if( false !== strpos($plugin_meta[$i], 'http://takahashifumiki.com') ){ |
| 177 |
$plugin_meta[$i] = str_replace('http://takahashifumiki.com', $this->ga_link('http://takahashifumiki.com', 'link'), $plugin_meta[$i]); |
| 178 |
} |
| 179 |
} |
| 180 |
$plugin_meta[] = sprintf('<a href="http://github.takahashifumiki.com/Gianism/">Github</a>'); |
| 181 |
} |
| 182 |
return $plugin_meta; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Generate Google Analytics ready URL |
| 187 |
* |
| 188 |
* @ignore |
| 189 |
*/ |
| 190 |
public function ga_link($link, $media = 'link'){ |
| 191 |
$source = rawurlencode(str_replace('http://', '', home_url('', 'http'))); |
| 192 |
return $link."?utm_source={$source}&utm_medium={$media}&utm_campaign=Gianism"; |
| 193 |
} |
| 194 |
} |