| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC; |
| 4 |
|
| 5 |
use ISC\Image_Sources\Image_Sources_Admin_Scripts; |
| 6 |
|
| 7 |
/** |
| 8 |
* Initialize WP Admin |
| 9 |
*/ |
| 10 |
class Admin { |
| 11 |
/** |
| 12 |
* Initiate admin functions |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
// load more admin-related classes |
| 16 |
add_action( 'init', [ $this, 'load_modules' ] ); |
| 17 |
|
| 18 |
// Remove existing admin notices and register our own |
| 19 |
add_action( 'in_admin_header', [ $this, 'unregister_admin_notices' ] ); |
| 20 |
|
| 21 |
// ISC page header |
| 22 |
add_action( 'isc_admin_notices', [ $this, 'branded_admin_header' ] ); |
| 23 |
|
| 24 |
// hide the admin language switcher from WPML on our pages |
| 25 |
add_filter( 'wpml_show_admin_language_switcher', [ $this, 'disable_wpml_admin_lang_switcher' ] ); |
| 26 |
|
| 27 |
// add links to setting and source list to plugin page |
| 28 |
add_action( 'plugin_action_links_' . ISCBASE, [ $this, 'add_links_to_plugin_page' ] ); |
| 29 |
|
| 30 |
// fire when an attachment is removed |
| 31 |
add_action( 'delete_attachment', [ $this, 'clear_unused_images_stats' ] ); |
| 32 |
|
| 33 |
new \ISC\Settings(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Load additional admin classes and modules |
| 38 |
*/ |
| 39 |
public function load_modules() { |
| 40 |
new \ISC\Admin\Admin_Scripts(); |
| 41 |
new \ISC\Admin\Admin_Ajax(); |
| 42 |
new \ISC\Admin\Media_Library_Filter(); |
| 43 |
new \ISC\Admin\Media_Library_Checks(); |
| 44 |
|
| 45 |
if ( Plugin::is_module_enabled( 'image_sources' ) ) { |
| 46 |
new \ISC\Image_Sources\Admin(); |
| 47 |
} |
| 48 |
|
| 49 |
new \ISC\Feedback(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Unregister existing admin notices and register our own |
| 54 |
* This should help prevent unrelated notices on ISC specific pages |
| 55 |
*/ |
| 56 |
public function unregister_admin_notices() { |
| 57 |
if ( ! Admin_Utils::is_isc_page() ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Remove all admin_notices hooks |
| 62 |
remove_all_actions( 'admin_notices' ); |
| 63 |
|
| 64 |
// Trigger ISC-specific admin notices action |
| 65 |
do_action( 'isc_admin_notices' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Add links to pages from plugins.php |
| 70 |
* |
| 71 |
* @param array $links existing plugin links. |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function add_links_to_plugin_page( $links ): array { |
| 75 |
// add link to premium. |
| 76 |
if ( ! Plugin::is_pro() ) { |
| 77 |
array_unshift( $links, Admin_Utils::get_pro_link( 'plugin-overview' ) ); |
| 78 |
} |
| 79 |
// settings link |
| 80 |
$links[] = sprintf( |
| 81 |
'<a href="%s">%s</a>', |
| 82 |
esc_url( add_query_arg( 'page', 'isc-settings', get_admin_url() . 'options-general.php' ) ), |
| 83 |
__( 'Settings', 'image-source-control-isc' ) |
| 84 |
); |
| 85 |
|
| 86 |
return $links; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Disable the WPML language switcher on ISC pages |
| 91 |
* |
| 92 |
* @param bool $state current state. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function disable_wpml_admin_lang_switcher( $state ): bool { |
| 97 |
// needs to run before plugins_loaded with prio 1, so our own `is_isc_page` function is not available here |
| 98 |
global $pagenow; |
| 99 |
|
| 100 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 101 |
if ( ! isset( $_GET['page'] ) ) { |
| 102 |
return $state; |
| 103 |
} |
| 104 |
|
| 105 |
// settings page |
| 106 |
if ( |
| 107 |
$pagenow === 'options-general.php' |
| 108 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 109 |
&& in_array( $_GET['page'], [ 'isc-settings' ], true ) |
| 110 |
) { |
| 111 |
$state = false; |
| 112 |
} |
| 113 |
|
| 114 |
// media pages |
| 115 |
if ( |
| 116 |
$pagenow === 'upload.php' |
| 117 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 118 |
&& in_array( $_GET['page'], [ 'isc-unused-images', 'isc-images' ], true ) |
| 119 |
) { |
| 120 |
$state = false; |
| 121 |
} |
| 122 |
|
| 123 |
return $state; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Add an ISC branded header to plugin pages |
| 128 |
*/ |
| 129 |
public static function branded_admin_header() { |
| 130 |
$screen = get_current_screen(); |
| 131 |
$screen_id = $screen->id ?? null; |
| 132 |
if ( ! Admin_Utils::is_isc_page() ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
switch ( $screen_id ) { |
| 136 |
case 'settings_page_isc-settings': |
| 137 |
$title = __( 'Settings', 'image-source-control-isc' ); |
| 138 |
break; |
| 139 |
case 'media_page_isc-sources': |
| 140 |
$title = __( 'Tools', 'image-source-control-isc' ); |
| 141 |
break; |
| 142 |
default: |
| 143 |
$title = get_admin_page_title(); |
| 144 |
} |
| 145 |
include ISCPATH . 'admin/templates/header.php'; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Actions to perform when an attachment is removed |
| 150 |
*/ |
| 151 |
public function clear_unused_images_stats() { |
| 152 |
// remove the transient with the unused image numbers |
| 153 |
delete_transient( 'isc-unused-attachments-stats' ); |
| 154 |
} |
| 155 |
} |
| 156 |
|