-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove redundant code #33
Comments
URLs with GET-Params should be printed out with WP-Core-functions:
Additionally the duplicated Instead of using multiple methods within the Init-class we should introduce a new class (e.G. PageManager) which contains all pages and handles the rendering of them. The page-classes should implement an interface. PageInternfaceinterface PageInterface {
public function get_menu_title();
public function get_page_title();
public function render();
} PageManagerclass PageManager {
/**
* @var PageInterface[]
*/
private $pages;
public function add( PageInterface $page ) {
$slug = sanitize_title_with_dashes( $page->get_menu_title() );
$this->pages[ $slug ] = $page
}
public function register() {
$cap = apply_filters( 'insr-capability', 'install_plugins' );
foreach ( $this->pages as $slug => $page ) {
add_submenu_page(
'tools.php',
$page->get_page_title(),
$page->get_menu_title(),
$cap,
$slug
array( $this, 'render' )
);
}
}
public function render() {
$url = admin_url( 'tools.php' );
$current_page = $_GET[ 'page' ] ) ? $_GET[ 'page' ] : key( $this->pages );
$output = '<ul class="tab__list">';
foreach( $this->pages as $slug => $page ) :
$class = $current_page === $slug ? 'tab__item--is-active' : '';
$output .= sprintf(
'<li class="tab__item %1$s"><a href="%2$s">%3$s</a></li>'
esc_attr( $class ),
add_query_arg( 'page', $slug, $url ),
$page->get_page_title()
);
endforeach;
$output .= '</ul>';
echo $output;
echo '<div class="tab__content">';
$this->pages[ $current_page ]->render();
echo '</div>';
}
} Usageclass ReplaceDomainAdmin implements PageInterface {
public function get_page_title() { __( 'Replace Domain', '..' ); }
public function get_menu_title() { /* snip */ }
public function render() { /* snip */ }
}
$manager = new PageManager();
$manger->add( new ReplaceDomainAdmin() );
add_action( 'admin_menu', array( $manager , 'register' ) ); Edit: The naming should also be improved. The class Example: Folder: |
solved |
The handling of admin pages are very prone for errors because the code repeats oneself.
Look at the tabs, we have the same markup on 5 different files:
https://github.com/inpsyde/search-and-replace/tree/3.0.1/src/inc/templates
Go a head to
src/inc/Init.php
here we have identical functions.https://github.com/inpsyde/search-and-replace/blob/3.0.1/src/inc/Init.php
TODO: Reduce code and implement better tab handling.
The text was updated successfully, but these errors were encountered: