| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
|
| 7 |
/** |
| 8 |
* Functions specific to the administration interface |
| 9 |
* |
| 10 |
* @package Code_Snippets |
| 11 |
*/ |
| 12 |
class Admin { |
| 13 |
|
| 14 |
/** |
| 15 |
* Admin_Menu class instances |
| 16 |
* |
| 17 |
* @var array<string, Admin_Menu> |
| 18 |
*/ |
| 19 |
public $menus = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* Class constructor |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
if ( is_admin() ) { |
| 26 |
$this->run(); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialise classes |
| 32 |
*/ |
| 33 |
public function load_classes() { |
| 34 |
$this->menus['manage'] = new Manage_Menu(); |
| 35 |
$this->menus['edit'] = new Edit_Menu(); |
| 36 |
$this->menus['import'] = new Import_Menu(); |
| 37 |
|
| 38 |
if ( is_network_admin() === Settings\are_settings_unified() ) { |
| 39 |
$this->menus['settings'] = new Settings_Menu(); |
| 40 |
} |
| 41 |
|
| 42 |
foreach ( $this->menus as $menu ) { |
| 43 |
$menu->run(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Register action and filter hooks |
| 49 |
*/ |
| 50 |
public function run() { |
| 51 |
add_action( 'init', array( $this, 'load_classes' ), 11 ); |
| 52 |
|
| 53 |
add_filter( 'mu_menu_items', array( $this, 'mu_menu_items' ) ); |
| 54 |
add_filter( 'plugin_action_links_' . plugin_basename( PLUGIN_FILE ), array( $this, 'plugin_action_links' ), 10, 2 ); |
| 55 |
add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); |
| 56 |
add_filter( 'debug_information', array( $this, 'debug_information' ) ); |
| 57 |
add_action( 'code_snippets/admin/manage', array( $this, 'print_notices' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Allow super admins to control site admin access to |
| 62 |
* snippet admin menus |
| 63 |
* |
| 64 |
* Adds a checkbox to the *Settings > Network Settings* |
| 65 |
* network admin menu |
| 66 |
* |
| 67 |
* @param array<string, string> $menu_items Current mu menu items. |
| 68 |
* |
| 69 |
* @return array<string, string> The modified mu menu items |
| 70 |
* |
| 71 |
* @since 1.7.1 |
| 72 |
*/ |
| 73 |
public function mu_menu_items( array $menu_items ): array { |
| 74 |
$menu_items['snippets'] = __( 'Snippets', 'code-snippets' ); |
| 75 |
$menu_items['snippets_settings'] = __( 'Snippets » Settings', 'code-snippets' ); |
| 76 |
|
| 77 |
return $menu_items; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Modify the action links for this plugin. |
| 82 |
* |
| 83 |
* @param array<string> $actions Existing plugin action links. |
| 84 |
* @param string $plugin_file The plugin the links are for. |
| 85 |
* |
| 86 |
* @return array<string> Modified plugin action links. |
| 87 |
* @since 2.0.0 |
| 88 |
*/ |
| 89 |
public function plugin_action_links( array $actions, string $plugin_file ): array { |
| 90 |
if ( plugin_basename( PLUGIN_FILE ) !== $plugin_file ) { |
| 91 |
return $actions; |
| 92 |
} |
| 93 |
|
| 94 |
$format = '<a href="%1$s" title="%2$s">%3$s</a>'; |
| 95 |
|
| 96 |
return array_merge( |
| 97 |
[ |
| 98 |
sprintf( |
| 99 |
$format, |
| 100 |
esc_url( code_snippets()->get_menu_url( 'settings' ) ), |
| 101 |
esc_html__( 'Change plugin settings', 'code-snippets' ), |
| 102 |
esc_html__( 'Settings', 'code-snippets' ) |
| 103 |
), |
| 104 |
sprintf( |
| 105 |
$format, |
| 106 |
esc_url( code_snippets()->get_menu_url() ), |
| 107 |
esc_html__( 'Manage your existing snippets', 'code-snippets' ), |
| 108 |
esc_html__( 'Snippets', 'code-snippets' ) |
| 109 |
), |
| 110 |
], |
| 111 |
$actions, |
| 112 |
[ |
| 113 |
sprintf( |
| 114 |
'<a href="%1$s" title="%2$s" style="color: #d46f4d; font-weight: bold;" target="_blank">%3$s</a>', |
| 115 |
'https://snipco.de/JE2i', |
| 116 |
esc_attr__( 'Upgrade to Code Snippets Pro', 'code-snippets' ), |
| 117 |
esc_html__( 'Go Pro', 'code-snippets' ) |
| 118 |
), |
| 119 |
] |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Adds extra links related to the plugin |
| 125 |
* |
| 126 |
* @param array<string> $plugin_meta Existing plugin info links. |
| 127 |
* @param string $plugin_file The plugin the links are for. |
| 128 |
* |
| 129 |
* @return array<string> Modified plugin info links. |
| 130 |
* @since 2.0.0 |
| 131 |
*/ |
| 132 |
public function plugin_row_meta( array $plugin_meta, string $plugin_file ): array { |
| 133 |
if ( plugin_basename( PLUGIN_FILE ) !== $plugin_file ) { |
| 134 |
return $plugin_meta; |
| 135 |
} |
| 136 |
|
| 137 |
$format = '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>'; |
| 138 |
|
| 139 |
return array_merge( |
| 140 |
$plugin_meta, |
| 141 |
array( |
| 142 |
sprintf( |
| 143 |
$format, |
| 144 |
'https://help.codesnippets.pro/', |
| 145 |
esc_attr__( 'Find out how to get support with Code Snippets', 'code-snippets' ), |
| 146 |
esc_html__( 'Docs and Support', 'code-snippets' ) |
| 147 |
), |
| 148 |
sprintf( |
| 149 |
$format, |
| 150 |
'https://www.facebook.com/groups/codesnippetsplugin/', |
| 151 |
esc_attr__( 'Join our community on Facebook', 'code-snippets' ), |
| 152 |
esc_html__( 'Community', 'code-snippets' ) |
| 153 |
), |
| 154 |
) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Add Code Snippets information to Site Health information. |
| 160 |
* |
| 161 |
* @param array<string, array<string, mixed>> $info Current Site Health information. |
| 162 |
* |
| 163 |
* @return array<string, array<string, mixed>> Updated Site Health information. |
| 164 |
* @author sc0ttkclark |
| 165 |
*/ |
| 166 |
public function debug_information( array $info ): array { |
| 167 |
$fields = array(); |
| 168 |
|
| 169 |
// build the debug information from snippet data. |
| 170 |
foreach ( get_snippets() as $snippet ) { |
| 171 |
$values = [ $snippet->scope_name ]; |
| 172 |
$debug = []; |
| 173 |
|
| 174 |
if ( ! $snippet->active ) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
if ( $snippet->name ) { |
| 179 |
$debug[] = 'name: ' . $snippet->name; |
| 180 |
} |
| 181 |
|
| 182 |
$debug[] = 'scope: ' . $snippet->scope; |
| 183 |
|
| 184 |
if ( $snippet->modified ) { |
| 185 |
/* translators: %s: formatted last modified date */ |
| 186 |
$values[] = sprintf( __( 'Last modified %s', 'code-snippets' ), $snippet->format_modified( false ) ); |
| 187 |
$debug[] = 'modified: ' . $snippet->modified; |
| 188 |
} |
| 189 |
|
| 190 |
if ( $snippet->tags ) { |
| 191 |
$values[] = $snippet->tags_list; |
| 192 |
$debug[] = 'tags: [' . $snippet->tags_list . ']'; |
| 193 |
} |
| 194 |
|
| 195 |
$fields[ 'snippet-' . $snippet->id ] = [ |
| 196 |
'label' => $snippet->display_name, |
| 197 |
'value' => implode( "\n | ", $values ), |
| 198 |
'debug' => implode( ', ', $debug ), |
| 199 |
]; |
| 200 |
} |
| 201 |
|
| 202 |
$snippets_info = array( |
| 203 |
'label' => __( 'Active Snippets', 'code-snippets' ), |
| 204 |
'show_count' => true, |
| 205 |
'fields' => $fields, |
| 206 |
); |
| 207 |
|
| 208 |
// attempt to insert the new section right after the Inactive Plugins section. |
| 209 |
$index = array_search( 'wp-plugins-inactive', array_keys( $info ), true ); |
| 210 |
|
| 211 |
if ( false === $index ) { |
| 212 |
$info['code-snippets'] = $snippets_info; |
| 213 |
} else { |
| 214 |
$info = array_merge( |
| 215 |
array_slice( $info, 0, $index + 1 ), |
| 216 |
[ 'code-snippets' => $snippets_info ], |
| 217 |
array_slice( $info, $index + 1 ) |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
return $info; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Print any admin notices that have not been dismissed. |
| 226 |
* |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
public function print_notices() { |
| 230 |
global $current_user; |
| 231 |
|
| 232 |
$key = 'ignore_code_snippets_survey_message'; |
| 233 |
$dismissed = get_user_meta( $current_user->ID, $key ); |
| 234 |
|
| 235 |
if ( isset( $_GET[ $key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $key ) ) { |
| 236 |
add_user_meta( $current_user->ID, $key, sanitize_key( wp_unslash( $_GET[ $key ] ) ) ); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
$now = new DateTime(); |
| 241 |
$period_start = new DateTime( '2023-11-19 00:00:00' ); |
| 242 |
$period_end = new DateTime( '2023-12-01 00:00:00' ); |
| 243 |
|
| 244 |
if ( ! in_array( 'bf23', $dismissed, true ) && $now > $period_start && $now < $period_end ) { |
| 245 |
$notice = 'bf23'; |
| 246 |
$action_url = 'https://snipco.de/I_iW'; |
| 247 |
$action_label = __( 'Learn more', 'code-snippets' ); |
| 248 |
$text = __( "Don't miss out on our biggest Black Friday sale ever! Get 50% off on our plans.", 'code-snippets' ); |
| 249 |
|
| 250 |
} elseif ( ! in_array( 'pro', $dismissed, true ) ) { |
| 251 |
$notice = 'pro'; |
| 252 |
$action_url = 'https://codesnippets.pro/pricing/'; |
| 253 |
$action_label = __( 'Upgrade now', 'code-snippets' ); |
| 254 |
$text = __( '<strong>Code Snippets Pro is here!</strong> Find more about the features in Pro and our current offers.', 'code-snippets' ); |
| 255 |
|
| 256 |
} elseif ( ! in_array( 'survey', $dismissed, true ) && ! in_array( 'true', $dismissed, true ) ) { |
| 257 |
$notice = 'survey'; |
| 258 |
$action_url = 'https://codesnippets.pro/survey/'; |
| 259 |
$action_label = __( 'Take the survey now', 'code-snippets' ); |
| 260 |
$text = __( "<strong>Have feedback on Code Snippets?</strong> Please take the time to answer a short survey on how you use this plugin and what you'd like to see changed or added in the future.", 'code-snippets' ); |
| 261 |
} else { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
printf( |
| 266 |
'<div class="notice notice-info code-snippets-notice code-snippets-%s-notice is-dismissible"><p>', |
| 267 |
esc_attr( sanitize_key( $notice ) ) |
| 268 |
); |
| 269 |
|
| 270 |
echo wp_kses( $text, [ 'strong' => [] ] ); |
| 271 |
|
| 272 |
printf( |
| 273 |
'<a href="%s" class="button button-secondary" target="_blank" style="margin: auto .5em;">%s</a>', |
| 274 |
esc_url( $action_url ), |
| 275 |
esc_html( $action_label ) |
| 276 |
); |
| 277 |
|
| 278 |
printf( |
| 279 |
'<a href="%s" class="notice-dismiss"><span class="screen-reader-text">%s</span></a>', |
| 280 |
esc_url( wp_nonce_url( add_query_arg( $key, $notice ), $key ) ), |
| 281 |
esc_attr__( 'Dismiss', 'code-snippets' ) |
| 282 |
); |
| 283 |
|
| 284 |
echo '</p></div>'; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Render a nav tab for a snippet type. |
| 289 |
* |
| 290 |
* @param string $type_name Type identifier. |
| 291 |
* @param string $label Type label. |
| 292 |
* @param string $current_type Identifier of currently-selected type. |
| 293 |
* |
| 294 |
* @return void |
| 295 |
*/ |
| 296 |
public static function render_snippet_type_tab( string $type_name, string $label, string $current_type = '' ) { |
| 297 |
if ( $type_name === $current_type ) { |
| 298 |
printf( '<a class="nav-tab nav-tab-active" data-snippet-type="%s">', esc_attr( $type_name ) ); |
| 299 |
|
| 300 |
} elseif ( Plugin::is_pro_type( $type_name ) ) { |
| 301 |
printf( |
| 302 |
'<a class="nav-tab nav-tab-inactive" data-snippet-type="%s" title="%s" href="https://codesnippets.pro/pricing/" target="_blank">', |
| 303 |
esc_attr( $type_name ), |
| 304 |
esc_attr__( 'Available in Code Snippets Pro (external link)', 'code-snippets' ) |
| 305 |
); |
| 306 |
|
| 307 |
} else { |
| 308 |
$current_url = remove_query_arg( [ 'cloud_select', 'cloud_search' ] ); |
| 309 |
|
| 310 |
printf( |
| 311 |
'<a class="nav-tab" href="%s" data-snippet-type="%s">', |
| 312 |
esc_url( add_query_arg( 'type', $type_name, $current_url ) ), |
| 313 |
esc_attr( $type_name ) |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
echo esc_html( $label ); |
| 318 |
|
| 319 |
switch ( $type_name ) { |
| 320 |
case 'all': |
| 321 |
break; |
| 322 |
case 'cloud': |
| 323 |
echo '<span class="cloud-badge dashicons dashicons-cloud cloud-icon cloud-synced"></span>'; |
| 324 |
break; |
| 325 |
case 'cloud_search': |
| 326 |
echo '<span class="cloud-badge dashicons dashicons-search cloud-icon cloud-downloaded"></span>'; |
| 327 |
break; |
| 328 |
case 'bundles': |
| 329 |
echo '<span class="cloud-badge dashicons dashicons-screenoptions cloud-icon cloud-bundle"></span>'; |
| 330 |
break; |
| 331 |
case 'ai': |
| 332 |
echo '<span class="cloud-badge ai-icon">', esc_html__( 'AI', 'code-snippets' ), '</span>'; |
| 333 |
break; |
| 334 |
default: |
| 335 |
echo '<span class="badge">' . esc_html( $type_name ) . '</span>'; |
| 336 |
break; |
| 337 |
} |
| 338 |
|
| 339 |
echo '</a>'; |
| 340 |
} |
| 341 |
} |
| 342 |
|