From 00244c53abf5612a6ce92556118afef467294921 Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Tue, 20 Sep 2016 18:05:32 +0100 Subject: [PATCH] removes the admin in favour of Mercator-GUI --- admin.php | 537 --------------------------- inc/admin/class-alias-list-table.php | 244 ------------ mercator.php | 12 - 3 files changed, 793 deletions(-) delete mode 100644 admin.php delete mode 100644 inc/admin/class-alias-list-table.php diff --git a/admin.php b/admin.php deleted file mode 100644 index 31b3f26..0000000 --- a/admin.php +++ /dev/null @@ -1,537 +0,0 @@ - title - * @return array - */ -function add_site_list_column( $columns ) { - $columns['mercator_aliases'] = __( 'Aliases', 'mercator' ); - return $columns; -} - -/** - * Output the site list column - * - * @param string $column Column ID - * @param int $site_id Site ID - */ -function output_site_list_column( $column, $site_id ) { - switch ( $column ) { - case 'mercator_aliases': - $mappings = Mapping::get_by_site( $site_id ); - if ( ! empty( $mappings ) ) { - foreach ( $mappings as $mapping ) { - // Kinda horrible formatting, but matches the existing - echo esc_html( $mapping->get_domain() ) . '
'; - } - } - - break; - } -} - -/** - * Output the site tab if we're on the right page - * - * Outputs the link, then moves it into place using JS, as there are no hooks to - * speak of. - */ -function maybe_output_site_tab() { - if ( ! is_network_admin() ) { - return; - } - - if ( $GLOBALS['parent_file'] !== 'sites.php' || $GLOBALS['submenu_file'] !== 'sites.php' ) { - return; - } - - $id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0; - if ( empty( $id ) ) { - return; - } - - $class = ( ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'mercator-aliases' ) ? ' nav-tab-active' : ''; - -?> - - -%2$s'), get_blogaddress_by_id( $id ), $site_url_no_http ); - - // Load the page header - global $title, $parent_file, $submenu_file; - $title = sprintf( __( 'Aliases: %s', 'mercator' ), $site_url_no_http ); - $parent_file = 'sites.php'; - $submenu_file = 'sites.php'; - require_once(ABSPATH . 'wp-admin/admin-header.php'); - - $add_link = add_query_arg( - array( - 'action' => 'mercator-add', - 'id' => $id, - ), - network_admin_url( 'admin.php' ) - ); -?> - -
-

- - - - -

- -

' . $msg . '

'; - } -} - -/** - * Output the admin page footer - */ -function output_page_footer() { - echo ''; - - require_once(ABSPATH . 'wp-admin/admin-footer.php'); -} - -/** - * Handle submission of the list page - * - * Handles bulk actions for the list page. Redirects back to itself after - * processing, and exits. - * - * @param int $id Site ID - * @param string $action Action to perform - */ -function handle_list_page_submit( $id, $action ) { - check_admin_referer( 'mercator-aliases-bulk-' . $id ); - - $sendback = remove_query_arg( array( 'did_action', 'mappings', '_wpnonce' ), wp_get_referer() ); - if ( ! $sendback ) - $sendback = admin_url( $parent_file ); - - $mappings = empty( $_REQUEST['mappings'] ) ? array() : (array) $_REQUEST['mappings']; - $mappings = array_map( 'absint', $mappings ); - - if ( ! isset( $mappings ) ) { - wp_redirect( $sendback ); - exit; - } - - $processed = 0; - $args = array( - 'did_action' => $action, - 'mappings' => join( ',', $mappings ), - ); - switch ( $action ) { - case 'activate': - foreach ( $mappings as $id ) { - $mapping = Mapping::get( $id ); - if ( is_wp_error( $mapping ) ) { - continue; - } - - if ( $mapping->set_active( true ) ) { - $processed++; - } - } - break; - - case 'deactivate': - foreach ( $mappings as $id ) { - $mapping = Mapping::get( $id ); - if ( is_wp_error( $mapping ) ) { - continue; - } - - if ( $mapping->set_active( false ) ) { - $processed++; - } - } - break; - - case 'delete': - $args['domains'] = array(); - foreach ( $mappings as $id ) { - $mapping = Mapping::get( $id ); - if ( is_wp_error( $mapping ) ) { - continue; - } - - if ( $mapping->delete() ) { - // Mappings don't exist after we delete them, so pass the - // domain for messages and such - $args['domains'][] = $mapping->get_domain(); - $processed++; - } - } - break; - - default: - do_action_ref_array( "mercator_aliases_bulk_action-{$action}", array( $mappings, &$processed, $action ) ); - break; - } - - $args['processed'] = $processed; - $sendback = add_query_arg( $args, $sendback ); - - wp_safe_redirect( $sendback ); - exit(); -} - -/** - * Output alias editing page - */ -function output_list_page() { - - $id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; - - if ( ! $id ) - wp_die( __('Invalid site ID.') ); - - $id = absint( $id ); - - $details = get_blog_details( $id ); - if ( ! can_edit_network( $details->site_id ) || (int) $details->blog_id !== $id ) - wp_die( __( 'You do not have permission to access this page.' ) ); - - $wp_list_table = new Alias_List_Table( array( - 'site_id' => $id, - ) ); - - $messages = array(); - - $bulk_action = $wp_list_table->current_action(); - if ( $bulk_action ) { - $messages = handle_list_page_submit( $id, $bulk_action ); - } - - $pagenum = $wp_list_table->get_pagenum(); - - $wp_list_table->prepare_items( $id ); - - // Add messages for bulk actions - if ( ! empty( $_REQUEST['did_action'] ) ) { - $processed = empty( $_REQUEST['processed'] ) ? 0 : absint( $_REQUEST['processed'] ); - $did_action = $_REQUEST['did_action']; - - $mappings = empty( $_REQUEST['mappings'] ) ? array() : wp_parse_id_list( $_REQUEST['mappings'] ); - $mappings = array_map( 'absint', $mappings ); - - // Special case for single, as it's not really a "bulk" action - if ( $processed === 1 ) { - $bulk_messages = array( - 'activate' => __( 'Activated %s', 'mercator' ), - 'deactivate' => __( 'Deactivated %s', 'mercator' ), - 'delete' => __( 'Deleted %s', 'mercator' ), - 'add' => __( 'Created %s', 'mercator' ), - 'edit' => __( 'Updated %s', 'mercator' ), - ); - if ( $did_action !== 'delete' ) { - $mapping = Mapping::get( $mappings[0] ); - $domain = $mapping->get_domain(); - } - else { - $domain = empty( $_REQUEST['domains'] ) ? array() : $_REQUEST['domains'][0]; - } - $placeholder = '' . $domain . ''; - } - else { - // Note: we still use _n for languages which have special cases on - // e.g. 3, 5, 10, etc - $bulk_messages = array( - 'activate' => _n( '%s alias activated.', '%s aliases activated.', $processed ), - 'deactivate' => _n( '%s alias deactivated.', '%s aliases deactiaved.', $processed ), - 'delete' => _n( '%s alias deleted.', '%s aliases deleted.', $processed ), - 'add' => _n( '%s alias created.', '%s aliases created.', $processed ), - 'edit' => _n( '%s alias updated.', '%s aliases updated.', $processed ), - ); - $placeholder = number_format_i18n( $processed ); - } - $bulk_messages = apply_filters( 'mercator_aliases_bulk_messages', $bulk_messages, $processed ); - - if ( ! empty( $bulk_messages[ $did_action ] ) ) { - $messages[] = sprintf( $bulk_messages[ $did_action ], $placeholder ); - } - } - - output_page_header( $id, $messages ); - -?> -
- display(); ?> -
-site_id ) ) { - return new WP_Error( 'mercator.params.cannot_edit', __( 'You do not have permission to edit this site', 'mercator' ) ); - } - } - - // Validate active flag - $valid['active'] = empty( $params['active'] ) ? false : true; - - return $valid; -} - -/** - * Handle submission of the add page - * - * @return array|null List of errors. Issues a redirect and exits on success. - */ -function handle_edit_page_submit( $id, $mapping ) { - $messages = array(); - if ( empty( $mapping ) ) { - $did_action = 'add'; - check_admin_referer( 'mercator-add-' . $id ); - } - else { - $did_action = 'edit'; - check_admin_referer( 'mercator-edit-' . $mapping->get_id() ); - } - - // Check that the parameters are correct first - $params = validate_alias_parameters( wp_unslash( $_POST ) ); - if ( is_wp_error( $params ) ) { - $messages[] = $params->get_error_message(); - - if ( $params->get_error_code() === 'mercator.params.domain_invalid_chars' ) { - $messages[] = __( 'Note: for internationalized domain names, use the ASCII form (e.g, xn--bcher-kva.example)', 'mercator' ); - } - - return $messages; - } - if ( empty( $mapping ) ) { - // Create the actual mapping - $result = $mapping = Mapping::create( $params['site'], $params['domain'], $params['active'] ); - } - else { - // Update our existing - $result = $mapping->update( $params ); - } - - if ( is_wp_error( $result ) ) { - $messages[] = $result->get_error_message(); - - return $messages; - } - - // Success, redirect to alias page - $location = add_query_arg( - array( - 'action' => 'mercator-aliases', - 'id' => $id, - 'did_action' => $did_action, - 'mappings' => $mapping->get_id(), - 'processed' => 1, - '_wpnonce' => wp_create_nonce( 'mercator-alias-added-' . $mapping->get_id() ), - ), - network_admin_url( 'admin.php' ) - ); - wp_safe_redirect( $location ); - exit; -} - -/** - * Output alias editing page - */ -function output_edit_page() { - - $id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; - - if ( ! $id ) - wp_die( __('Invalid site ID.') ); - - $id = absint( $id ); - - $details = get_blog_details( $id ); - if ( ! can_edit_network( $details->site_id ) || (int) $details->blog_id !== $id ) - wp_die( __( 'You do not have permission to access this page.' ) ); - - // Are we editing? - $mapping = null; - $form_action = network_admin_url( 'admin.php?action=mercator-add' ); - if ( ! empty( $_REQUEST['mapping'] ) ) { - $mapping_id = absint( $_REQUEST['mapping'] ); - $mapping = Mapping::get( $mapping_id ); - if ( is_wp_error( $mapping ) || empty( $mapping ) ) { - wp_die( __( 'Invalid alias ID.', 'mercator' ) ); - } - - $form_action = network_admin_url( 'admin.php?action=mercator-edit' ); - } - - // Handle form submission - $messages = array(); - if ( ! empty( $_POST['submit'] ) ) { - $messages = handle_edit_page_submit( $id, $mapping ); - } - - output_page_header( $id, $messages ); - - if ( empty( $mapping ) || ! empty( $_POST['_wpnonce'] ) ) { - $domain = empty( $_POST['domain'] ) ? '' : wp_unslash( $_POST['domain'] ); - $active = ! empty( $_POST['active'] ); - } - else { - $domain = $mapping->get_domain(); - $active = $mapping->is_active(); - } - -?> -
- - - - - - - - - -
- - - -
- - - -
- - - get_id() ) . '" />'; - wp_nonce_field( 'mercator-edit-' . $mapping->get_id() ); - submit_button( __( 'Save Alias', 'mercator' ) ); - } - - ?> -
-' . sprintf( - __( 'Enhanced by Mercator', 'mercator' ), - 'https://github.com/humanmade/Mercator', - sprintf( - __( 'Version %s', 'mercator' ), - \Mercator\VERSION - ) - ) . ''; - array_unshift( $meta, $note ); - return $meta; -} diff --git a/inc/admin/class-alias-list-table.php b/inc/admin/class-alias-list-table.php deleted file mode 100644 index bd66fc2..0000000 --- a/inc/admin/class-alias-list-table.php +++ /dev/null @@ -1,244 +0,0 @@ -items = array(); - - if ( empty( $this->_args['site_id'] ) ) { - return; - } - - $id = $this->_args['site_id']; - $mappings = Mapping::get_by_site( $id ); - if ( is_wp_error( $mappings ) ) { - \Mercator\warn_with_message( __( 'Could not fetch aliases for the site. This may indicate a database error.', 'mercator' ) ); - } - if ( ! empty( $mappings ) ) { - $this->items = $mappings; - } - } - - /** - * Get columns for the table - * - * @return array Map of column ID => title - */ - public function get_columns() { - return array( - 'cb' => '', - 'domain' => _x( 'Domain', 'mercator' ), - 'active' => _x( 'Active', 'mercator' ), - ); - } - - /** - * Get an associative array ( option_name => option_title ) with the list - * of bulk actions available on this table. - * - * @since 3.1.0 - * @access protected - * - * @return array - */ - protected function get_bulk_actions() { - $actions = array( - 'activate' => _x( 'Activate', 'mercator' ), - 'deactivate' => _x( 'Deactivate', 'mercator' ), - 'delete' => _x( 'Delete', 'mercator' ), - ); - - return apply_filters( 'mercator_alias_bulk_actions', $actions ); - } - - /** - * Display the bulk actions dropdown. - * - * @since 3.1.0 - * @access protected - * - * @param string $which The location of the bulk actions: 'top' or 'bottom'. - * This is designated as optional for backwards-compatibility. - */ - protected function bulk_actions( $which = '' ) { - if ( is_null( $this->_actions ) ) { - $no_new_actions = $this->_actions = $this->get_bulk_actions(); - /** - * Filter the list table Bulk Actions drop-down. - * - * The dynamic portion of the hook name, $this->screen->id, refers - * to the ID of the current screen, usually a string. - * - * This filter can currently only be used to remove bulk actions. - * - * @since 3.5.0 - * - * @param array $actions An array of the available bulk actions. - */ - $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); - $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions ); - $two = ''; - echo ''; - wp_nonce_field( 'mercator-aliases-bulk-' . $this->_args['site_id'] ); - } else { - $two = '2'; - } - - if ( empty( $this->_actions ) ) - return; - - echo ""; - echo "\n"; - - submit_button( __( 'Apply' ), 'action', false, false, array( 'id' => "doaction$two" ) ); - echo "\n"; - } - - /** - * Get the current action selected from the bulk actions dropdown. - * - * @since 3.1.0 - * @access public - * - * @return string|bool The action name or False if no action was selected - */ - public function current_action() { - if ( isset( $_REQUEST['bulk_action'] ) && -1 != $_REQUEST['bulk_action'] ) - return $_REQUEST['bulk_action']; - - if ( isset( $_REQUEST['bulk_action2'] ) && -1 != $_REQUEST['bulk_action2'] ) - return $_REQUEST['bulk_action2']; - - return false; - } - - /** - * Output extra "navigation" fields (section before/after the table) - * - * Outputs our Add New button above the table - * - * @param string $which Which tablenav to use (top/bottom) - */ - protected function extra_tablenav( $which ) { - global $status; - - if ( $which !== 'top' ) - return; - - $add_link = add_query_arg( - array( - 'action' => 'mercator-add', - 'id' => $this->_args['site_id'], - ), - network_admin_url( 'admin.php' ) - ); - echo '
'; - echo '' . esc_html_x( 'Add New', 'alias', 'mercator' ) . ''; - echo '
'; - } - - /** - * Get cell value for the checkbox column - * - * @param Mapping $mapping Current mapping item - * @return string HTML for the cell - */ - protected function column_cb( $mapping ) { - return '' - . ''; - } - - /** - * Get cell value for the domain column - * - * @param Mapping $mapping Current mapping item - * @return string HTML for the cell - */ - protected function column_domain( $mapping ) { - $domain = esc_html( $mapping->get_domain() ); - if ( substr( $domain, 0, 4 ) === 'www.' ) { - $domain = substr( $domain, 4 ); - } - - $args = array( - 'action' => 'mercator-aliases', - 'id' => $mapping->get_site_id(), - 'mappings' => $mapping->get_id(), - '_wpnonce' => wp_create_nonce( 'mercator-aliases-bulk-' . $this->_args['site_id'] ), - ); - if ( ! $mapping->is_active() ) { - $text = __( 'Activate', 'mercator' ); - $action = 'activate'; - } - else { - $text = __( 'Deactivate', 'mercator' ); - $action = 'deactivate'; - } - $args['bulk_action'] = $action; - - $link = add_query_arg( $args, network_admin_url( 'admin.php' ) ); - - $delete_args = $args; - $delete_args['bulk_action'] = 'delete'; - $delete_link = add_query_arg( $delete_args, network_admin_url( 'admin.php' ) ); - - $edit_link = add_query_arg( - array( - 'action' => 'mercator-edit', - 'id' => $mapping->get_site_id(), - 'mapping' => $mapping->get_id(), - ), - network_admin_url( 'admin.php' ) - ); - - $actions = array( - 'edit' => sprintf( '%s', esc_url( $edit_link ), esc_html__( 'Edit', 'mercator' ) ), - $action => sprintf( '%s', esc_url( $link ), esc_html( $text ) ), - 'delete' => sprintf( '%s', esc_url( $delete_link ), esc_html__( 'Delete', 'mercator' ) ), - ); - $actions = apply_filters( 'mercator_alias_actions', $actions, $mapping ); - $action_html = $this->row_actions( $actions, false ); - - return '' . $domain . '' . $action_html; - } - - /** - * Get cell value for the active column - * - * @param Mapping $mapping Current mapping item - * @return string HTML for the cell - */ - protected function column_active( $mapping ) { - $active = $mapping->is_active(); - if ( $active ) { - return esc_html__( 'Active', 'mercator' ); - } - return esc_html__( 'Inactive', 'mercator'); - } -} diff --git a/mercator.php b/mercator.php index d34fe5b..61d3a78 100644 --- a/mercator.php +++ b/mercator.php @@ -89,7 +89,6 @@ function startup() { // Actually hook in! add_filter( 'pre_get_site_by_path', __NAMESPACE__ . '\\check_domain_mapping', 10, 2 ); - add_action( 'admin_init', __NAMESPACE__ . '\\load_admin', -100 ); add_action( 'delete_blog', __NAMESPACE__ . '\\clear_mappings_on_delete' ); add_action( 'muplugins_loaded', __NAMESPACE__ . '\\register_mapped_filters', -10 ); @@ -222,17 +221,6 @@ function check_table() { return 'created'; } -/** - * Load administration functions - * - * We do this here rather than just including it to avoid extra load on - * non-admin pages. - */ -function load_admin() { - require_once __DIR__ . '/admin.php'; - require_once __DIR__ . '/inc/admin/class-alias-list-table.php'; -} - /** * Clear mappings for a site when it's deleted *