PluginProbe
Gianism / 2.2.5
Gianism v2.2.5
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / app / admin.php

admin.php in Gianism 2.2.5, at app/admin.php

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