| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class used to load admin pages allowing child classes |
| 4 |
* to replace or add pages by changing the classes used. |
| 5 |
* |
| 6 |
* @package Search_Replace_WPCode |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class WFR admin page loader. |
| 11 |
*/ |
| 12 |
class WSRW_Admin_Page_Loader { |
| 13 |
|
| 14 |
/** |
| 15 |
* Array of admin pages to load. |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
public $pages = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* Slugs of pages that should not be visible in the submenu. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
public $hidden_pages = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
$this->require_files(); |
| 33 |
|
| 34 |
$this->hooks(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Hooks. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function hooks() { |
| 43 |
add_action( 'admin_menu', array( $this, 'register_admin_menu' ), 9 ); |
| 44 |
add_filter( 'plugin_action_links_' . WSRW_PLUGIN_BASENAME, array( $this, 'add_plugin_action_links' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Load required files for the admin pages. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function require_files() { |
| 53 |
require_once WSRW_PLUGIN_PATH . 'includes/admin/pages/class-wsrw-admin-page.php'; |
| 54 |
require_once WSRW_PLUGIN_PATH . 'includes/admin/pages/class-wsrw-admin-page-search-replace.php'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Load the pages classes allowing child classes to replace. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function prepare_pages() { |
| 63 |
$this->pages['search_replace'] = 'WSRW_Admin_Page_Search_Replace'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Load the pages using their specific classes. |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function load_pages() { |
| 72 |
|
| 73 |
$this->prepare_pages(); |
| 74 |
|
| 75 |
do_action( 'wsrw_before_admin_pages_loaded', $this->pages ); |
| 76 |
|
| 77 |
foreach ( $this->pages as $page_class ) { |
| 78 |
if ( ! class_exists( $page_class ) ) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
/** |
| 82 |
* The page class. |
| 83 |
* |
| 84 |
* @var WSRW_Admin_Page $new_page |
| 85 |
*/ |
| 86 |
$new_page = new $page_class(); |
| 87 |
if ( $new_page->hide_menu ) { |
| 88 |
$this->hidden_pages[] = $new_page->page_slug; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Handler for registering the admin menu & loading pages. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function register_admin_menu() { |
| 99 |
$this->load_pages(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Generic handler for the wpcode pages. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function admin_menu_page() { |
| 108 |
do_action( 'wsrw_admin_page' ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Add a link to the code snippets list in the plugins list view. |
| 113 |
* |
| 114 |
* @param array $links The links specific to our plugin. |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function add_plugin_action_links( $links ) { |
| 119 |
$url = add_query_arg( |
| 120 |
array( |
| 121 |
'page' => 'wsrw-search-replace', |
| 122 |
), |
| 123 |
admin_url( 'tools.php' ) |
| 124 |
); |
| 125 |
$text = esc_html__( 'Search & Replace', 'search-replace-wpcode' ); |
| 126 |
|
| 127 |
$custom = array(); |
| 128 |
|
| 129 |
$custom['settings'] = sprintf( |
| 130 |
'<a href="%1$s">%2$s</a>', |
| 131 |
$url, |
| 132 |
$text |
| 133 |
); |
| 134 |
|
| 135 |
return array_merge( $custom, $links ); |
| 136 |
} |
| 137 |
} |
| 138 |
|