| 1 |
<?php |
| 2 |
|
| 3 |
namespace FakerPress; |
| 4 |
|
| 5 |
use FakerPress\Admin\Menu; |
| 6 |
use FakerPress\Admin\View\Factory; |
| 7 |
|
| 8 |
class Admin extends Template { |
| 9 |
/** |
| 10 |
* Variable holding the messages objects |
| 11 |
* |
| 12 |
* @since 0.1.2 |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
protected static $messages = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Variable holding the submenus objects |
| 20 |
* |
| 21 |
* @since 0.1.0 |
| 22 |
* |
| 23 |
* @var object |
| 24 |
*/ |
| 25 |
public static $view = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Makes it easier to check if is AJAX |
| 29 |
* |
| 30 |
* @since 0.1.2 |
| 31 |
* |
| 32 |
* @var bool |
| 33 |
*/ |
| 34 |
public static $is_ajax = false; |
| 35 |
|
| 36 |
public function is_active(): bool { |
| 37 |
$page = get_request_var( 'page' ); |
| 38 |
|
| 39 |
return ( ! is_null( $page ) && strtolower( $page ) === Plugin::$slug ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Static method to include all the Hooks for WordPress |
| 44 |
* There is a safe conditional here, it can only be triggered once! |
| 45 |
* |
| 46 |
* @since 0.1.0 |
| 47 |
* |
| 48 |
* @uses add_filter |
| 49 |
* |
| 50 |
* @uses add_action |
| 51 |
* @return null Construct never returns |
| 52 |
*/ |
| 53 |
public function __construct() { |
| 54 |
$this->set_template_origin( make( Plugin::class ) ) |
| 55 |
->set_template_folder( 'src/templates/pages' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Creating messages in a standard way |
| 60 |
* |
| 61 |
* @todo Move to the Page Abstract. |
| 62 |
* |
| 63 |
* @since 0.1.2 |
| 64 |
* |
| 65 |
* @param string $type The type of the Message |
| 66 |
* @param integer $priority The priority to show this message |
| 67 |
* |
| 68 |
* @param string $html HTML or text of the message |
| 69 |
*/ |
| 70 |
public static function add_message( $html, $type = 'success', $priority = 10 ) { |
| 71 |
$priority = absint( $priority ); |
| 72 |
|
| 73 |
/** |
| 74 |
* @filter fakerpress.messages.allowed_html |
| 75 |
* @since 0.1.2 |
| 76 |
*/ |
| 77 |
self::$messages[] = $message = (object) [ |
| 78 |
'html' => wp_kses( wpautop( $html ), apply_filters( 'fakerpress.messages.allowed_html', [] ), [ 'http', 'https' ] ), |
| 79 |
'type' => esc_attr( $type ), |
| 80 |
'priority' => $priority === 0 ? $priority + 1 : $priority, |
| 81 |
]; |
| 82 |
|
| 83 |
usort( self::$messages, 'FakerPress\sort_by_priority' ); |
| 84 |
|
| 85 |
return $message; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Method triggered to add messages recorded in this request to the admin front-end |
| 90 |
* |
| 91 |
* @todo Move to the Page Abstract. |
| 92 |
* |
| 93 |
* @since 0.1.2 |
| 94 |
* @return null Actions do not return |
| 95 |
*/ |
| 96 |
public function _action_admin_notices() { |
| 97 |
foreach ( self::$messages as $k => $message ) { |
| 98 |
$classes = [ |
| 99 |
// Plugin class to give the styling |
| 100 |
'fakerpress-message', |
| 101 |
// This is to use WordPress JS to move them above the h2 |
| 102 |
'notice', |
| 103 |
]; |
| 104 |
|
| 105 |
if ( 0 === $k ) { |
| 106 |
$classes[] = 'first'; |
| 107 |
} |
| 108 |
|
| 109 |
if ( $k + 1 === count( self::$messages ) ) { |
| 110 |
$classes[] = 'last'; |
| 111 |
} |
| 112 |
|
| 113 |
switch ( $message->type ) { |
| 114 |
case 'error': |
| 115 |
$classes[] = 'fakerpress-message-error'; |
| 116 |
break; |
| 117 |
case 'success': |
| 118 |
$classes[] = 'fakerpress-message-success'; |
| 119 |
break; |
| 120 |
case 'warning': |
| 121 |
$classes[] = 'fakerpress-message-warning'; |
| 122 |
break; |
| 123 |
default: |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
?> |
| 128 |
<div class="<?php echo wp_kses( implode( ' ', $classes ), [] ); ?>"><?php echo wp_kses( $message->html, apply_filters( 'fakerpress.messages.allowed_html', [] ), [ |
| 129 |
'http', |
| 130 |
'https' |
| 131 |
] ); ?></div> |
| 132 |
<?php |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Gets the base title for the admin pages. |
| 139 |
* |
| 140 |
* @since 0.6.0 |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public function get_base_title(): string { |
| 145 |
/** |
| 146 |
* Allows the modification of the base title for FakerPress pages. |
| 147 |
* |
| 148 |
* @since 0.6.0 |
| 149 |
* |
| 150 |
* @param string $title Which base title will be used. |
| 151 |
*/ |
| 152 |
return apply_filters( 'fakerpress.admin.title_base', __( '%s on FakerPress', 'fakerpress' ) ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @todo refactor |
| 157 |
*/ |
| 158 |
public function _filter_set_admin_page_title( $admin_title, $title ) { |
| 159 |
if ( ! $this->is_active() ) { |
| 160 |
return $admin_title; |
| 161 |
} |
| 162 |
$view = make( Factory::class )->get_current_view(); |
| 163 |
|
| 164 |
$pos = strpos( $admin_title, $title ); |
| 165 |
if ( false !== $pos ) { |
| 166 |
$admin_title = substr_replace( $admin_title, sprintf( $this->get_base_title(), $view->get_title() ), $pos, strlen( $title ) ); |
| 167 |
} |
| 168 |
|
| 169 |
return $admin_title; |
| 170 |
} |
| 171 |
|
| 172 |
public function _filter_messages_allowed_html() { |
| 173 |
return [ |
| 174 |
'a' => [ |
| 175 |
'class' => [], |
| 176 |
'href' => [], |
| 177 |
'title' => [] |
| 178 |
], |
| 179 |
'br' => [ |
| 180 |
'class' => [], |
| 181 |
], |
| 182 |
'p' => [ |
| 183 |
'class' => [], |
| 184 |
], |
| 185 |
'em' => [ |
| 186 |
'class' => [], |
| 187 |
], |
| 188 |
'strong' => [ |
| 189 |
'class' => [], |
| 190 |
], |
| 191 |
'b' => [ |
| 192 |
'class' => [], |
| 193 |
], |
| 194 |
'i' => [ |
| 195 |
'class' => [], |
| 196 |
], |
| 197 |
'ul' => [ |
| 198 |
'class' => [], |
| 199 |
], |
| 200 |
'ol' => [ |
| 201 |
'class' => [], |
| 202 |
], |
| 203 |
'li' => [ |
| 204 |
'class' => [], |
| 205 |
], |
| 206 |
]; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Filter the WordPress Version on plugins pages to display plugin version |
| 211 |
* |
| 212 |
* @since 0.1.0 |
| 213 |
* @uses __ |
| 214 |
* |
| 215 |
* @uses \FakerPress\Plugin::$slug |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
public function _filter_admin_footer_text( $text ) { |
| 219 |
if ( ! $this->is_active() ) { |
| 220 |
return $text; |
| 221 |
} |
| 222 |
|
| 223 |
return |
| 224 |
'<a target="_blank" href="http://wordpress.org/support/plugin/fakerpress#postform">' . esc_attr__( 'Contact Support', 'fakerpress' ) . '</a> | ' . |
| 225 |
str_replace( |
| 226 |
[ '[stars]', '[wp.org]' ], |
| 227 |
[ |
| 228 |
'<a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/fakerpress#postform" >★★★★★</a>', |
| 229 |
'<a target="_blank" href="http://wordpress.org/plugins/fakerpress/" >wordpress.org</a>' |
| 230 |
], |
| 231 |
__( 'Add your [stars] on [wp.org] to spread the love.', 'fakerpress' ) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Filter the WordPress Version on plugins pages to display the plugin version |
| 237 |
* |
| 238 |
* @since 0.1.0 |
| 239 |
* @uses \FakerPress\Plugin::admin_url |
| 240 |
* @uses \FakerPress\Plugin::VERSION |
| 241 |
* @uses __ |
| 242 |
* |
| 243 |
* @uses \FakerPress\Plugin::$slug |
| 244 |
* @return string |
| 245 |
*/ |
| 246 |
public function _filter_update_footer( $text ) { |
| 247 |
if ( ! $this->is_active() ) { |
| 248 |
return $text; |
| 249 |
} |
| 250 |
|
| 251 |
$sponsor = sprintf( |
| 252 |
'<a class="fp-link-footer fp-sponsor-link" href="%2$s" title="%3$s" target="_blank"><span class="dashicons dashicons-money-alt"></span> %1$s</a> | ', |
| 253 |
esc_html__( 'Sponsor the project on GitHub', 'fakerpress' ), |
| 254 |
Plugin::ext_site_url( '/r/sponsor' ), |
| 255 |
esc_attr__( 'Help by sponsoring the Project on GitHub', 'fakerpress' ) |
| 256 |
); |
| 257 |
$translate = sprintf( |
| 258 |
'<a class="fp-link-footer fp-translations-link" href="%2$s" title="%3$s" target="_blank"><span class="dashicons dashicons-translation"></span> %1$s</a> | ', |
| 259 |
esc_html__( 'Translate', 'fakerpress' ), |
| 260 |
Plugin::ext_site_url( '/r/translate' ), |
| 261 |
esc_attr__( 'Help us with Translations for the FakerPress project', 'fakerpress' ) |
| 262 |
); |
| 263 |
$version = sprintf( |
| 264 |
'<a class="fp-link-footer fp-version-link" href="%2$s" title="%3$s" target="_blank">%1$s</a>', |
| 265 |
esc_html__( 'Version: ', 'fakerpress' ) . esc_attr( Plugin::VERSION ), |
| 266 |
esc_url( Plugin::admin_url( 'view=changelog&version=' . esc_attr( Plugin::VERSION ) ) ), |
| 267 |
esc_attr__( 'View what changed in this version', 'fakerpress' ) |
| 268 |
); |
| 269 |
|
| 270 |
return $sponsor . $translate . $version; |
| 271 |
} |
| 272 |
|
| 273 |
public function _filter_body_class( $classes ) { |
| 274 |
$more = [ |
| 275 |
$classes, |
| 276 |
'__fakerpress', |
| 277 |
]; |
| 278 |
|
| 279 |
return implode( ' ', $more ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Filters the array of row meta for each plugin in the Plugins list table. |
| 284 |
* |
| 285 |
* @since 0.6.3 |
| 286 |
* |
| 287 |
* @param array<int, string> $plugin_meta An array of the plugin's metadata. |
| 288 |
* @param string $plugin_file Path to the plugin file relative to the plugins directory. |
| 289 |
* |
| 290 |
* @return array<int, string> Updated array of the plugin's metadata. |
| 291 |
*/ |
| 292 |
public function filter_plugin_row_meta( array $plugin_meta, $plugin_file ) { |
| 293 |
if ( 'fakerpress/fakerpress.php' !== $plugin_file ) { |
| 294 |
return $plugin_meta; |
| 295 |
} |
| 296 |
|
| 297 |
$plugin_meta[] = sprintf( |
| 298 |
'<a href="%1$s"><span class="dashicons dashicons-star-filled" aria-hidden="true" style="font-size:14px;line-height:1.3"></span>%2$s</a>', |
| 299 |
'https://fakerpress.com/r/sponsor', |
| 300 |
esc_html_x( 'Sponsor', 'verb', 'fakerpress' ) |
| 301 |
); |
| 302 |
|
| 303 |
return $plugin_meta; |
| 304 |
} |
| 305 |
} |
| 306 |
|