';
}
/**
* handle the settings field hide
*/
function admin_hide() {
echo '
settings['404page_hide'], false ) . '/>';
echo '' . __( 'Hide the selected page from the Pages list', '404page' ) . ' ';
echo ' ' . __( 'For Administrators the page is always visible.', '404page' ) . '
';
}
/**
* handle the settings field fire 404 error
*/
function admin_fire404() {
echo '
settings['404page_fire_error'], false ) . '/>';
echo '' . __( 'Send an 404 error if the page is accessed directly by its URL', '404page' ) . ' ';
echo ' ' . __( 'Uncheck this if you want the selected page to be accessible.', '404page' );
if ( function_exists( 'wpsupercache_activate' ) ) {
echo ' ' . __( 'WP Super Cache Plugin detected', '404page' ) . '. ' . __ ( 'If the page you selected as 404 error page is in cache, always a HTTP code 200 is sent. To avoid this and send a HTTP code 404 you have to exlcude this page from caching', '404page' ) . ' (' . __( 'Click here', '404page' ) . '). (' . __( 'Read more', '404page' ) . ')';
}
echo '
';
}
/**
* handle the settings field to force an 404 error
*/
function admin_force404() {
echo '
settings['404page_force_error'], false ) . '/>';
echo '' . __( 'Force 404 error after loading page', '404page' ) . ' ';
echo ' ' . __( 'Generally this is not needed. It is not recommended to activate this option, unless it is necessary. Please note that this may cause problems with your theme.', '404page' ) . '
';
}
/**
* handle the settings field to stop URL guessing
*/
function admin_noguess() {
echo '
settings['404page_no_url_guessing'], false ) . '/>';
echo '' . __( 'Disable URL autocorrection guessing', '404page' ) . ' ';
echo ' ' . __( 'This stops WordPress from URL autocorrection guessing. Only activate, if you are sure about the consequences.', '404page' ) . '
';
}
/**
* handle the settings field to send an http 410 error in case the object is trashed
* @since 3.2
*/
function admin_http410() {
echo '
settings['404page_http410_if_trashed'], false ) . '/>';
echo '' . __( 'Send an HTTP 410 error instead of HTTP 404 in case the requested object is in trash', '404page' ) . ' ';
echo ' ' . __( 'Check this if you want to inform search engines that the resource requested is no longer available and will not be available again so it can be removed from the search index immediately.', '404page' );
}
/**
* handle the settings field method
*/
function admin_method() {
if ( $this->settings['404page_native'] || defined( 'CUSTOMIZR_VER' ) || defined( 'ICL_SITEPRESS_VERSION' ) ) {
$dis = ' disabled="disabled"';
} else {
$dis = '';
}
echo '
settings['404page_method'], false ) . $dis . '/>';
echo '' . __( 'Activate Compatibility Mode', '404page' ) . ' ';
echo ' ';
if ( $this->settings['404page_native'] ) {
_e( 'This setting is not available because the Theme you are using natively supports the 404page plugin.', '404page' );
echo ' (' . __( 'Read more', '404page' ) . ')';
} elseif ( defined( 'CUSTOMIZR_VER' ) ) {
_e( 'This setting is not availbe because the 404page Plugin works in Customizr Compatibility Mode.', '404page' );
echo ' (' . __( 'Read more', '404page' ) . ')';
} elseif ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
_e( 'This setting is not availbe because the 404page Plugin works in WPML Mode.', '404page' );
echo ' (' . __( 'Read more', '404page' ) . ')';
} else {
_e( 'If you are using a theme or plugin that modifies the WordPress Template System, the 404page plugin may not work properly. Compatibility Mode maybe can fix the problem. Activate Compatibility Mode only if you have any problems.', '404page' );
}
echo '
';
}
/**
* handle the method setting
*/
function handle_method( $method ) {
if ( null === $method ) {
$method = 'STD';
}
return $method;
}
/**
* hide the 404 page from the list of pages
*/
function exclude_404page( $query ) {
$pageid = $this->get_page_id();
if ( $pageid > 0 ) {
global $pagenow;
$post_type = $query->get( 'post_type' );
// as of v 2.3 we check the post_type on front end
// as of v 2.5 we also hide the page from search results on front end
if( ( is_admin() && ( 'edit.php' == $pagenow && !current_user_can( 'create_users' ) ) ) || ( ! is_admin() && ( is_search() || ( !empty( $post_type) && ( ('page' === $post_type || 'any' === $post_type) || ( is_array( $post_type ) && in_array( 'page', $post_type ) ) ) ) ) ) ) {
// as of v 2.4 we hide all translations in admin for WPML
// as of v 2.5 we hide all translations from search results on front end for WPML
if ( is_admin() || ( ! is_admin() && is_search() ) ) {
$pageids = $this->get_all_page_ids();
} else {
$pageids = array( $pageid );
}
// as of v 2.3 we add the ID of the 404 page to post__not_in
// using just $query->set() overrides existing settings but not adds a new setting
$query->set( 'post__not_in', array_merge( (array)$query->get( 'post__not_in', array() ), $pageids ) );
}
}
}
/**
* remove the 404 page from get_pages result array
*/
function remove_404page_from_array( $pages, $r ) {
$pageid = $this->get_page_id();
if ( $pageid > 0 ) {
for ( $i = 0; $i < sizeof( $pages ); $i++ ) {
if ( $pages[$i]->ID == $pageid ) {
unset( $pages[$i] );
break;
}
}
}
return array_values( $pages );
}
/**
* check if the requested url is found in trash
* @since 3.2
* based on WP core function url_to_postid()
*/
function is_url_in_trash( $url ) {
global $wp_rewrite;
global $wp;
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) {
$id = absint( $values[2] );
if ( $id ) {
if ( 'trash' == get_post_status( $id ) ) {
return true;
} else {
return false;
}
}
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if ( empty( $rewrite ) ) {
return false;
}
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
// Get rid of URL ?query=string
$url_split = explode('?', $url);
$url = $url_split[0];
// Add 'www.' if it is absent and should be there
if ( false !== strpos( home_url(), '://www.' ) && false === strpos( $url, '://www.' ) ) {
$url = str_replace('://', '://www.', $url);
}
// Strip 'www.' if it is present and shouldn't be
if ( false === strpos( home_url(), '://www.' ) ) {
$url = str_replace('://www.', '://', $url);
}
// Strip 'index.php/' if we're not using path info permalinks
if ( !$wp_rewrite->using_index_permalinks() ) {
$url = str_replace( $wp_rewrite->index . '/', '', $url );
}
if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
// Chop off http://domain.com/[path]
$url = str_replace(home_url(), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url( home_url( '/' ) );
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
}
// Trim leading and lagging slashes
$url = trim($url, '/');
$request = $url;
$post_type_query_vars = array();
foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
if ( ! empty( $t->query_var ) ) {
$post_type_query_vars[ $t->query_var ] = $post_type;
}
}
// Look for matches.
$request_match = $request;
foreach ( (array)$rewrite as $match => $query) {
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if ( !empty( $url ) && ( $url != $request ) && ( strpos( $match, $url ) === 0 ) ) {
$request_match = $url . '/' . $request;
}
if ( preg_match( "#^$match#", $request_match, $matches ) ) {
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
// This is a verbose page match, let's check to be sure about it.
if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) ) {
continue;
}
}
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace( "!^.+\?!", '', $query );
// Substitute the substring matches into the query.
$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
// Filter out non-public query vars
parse_str( $query, $query_vars );
$query = array();
foreach ( (array) $query_vars as $key => $value ) {
if ( in_array( $key, $wp->public_query_vars ) ) {
$query[$key] = $value;
if ( isset( $post_type_query_vars[$key] ) ) {
$query['post_type'] = $post_type_query_vars[$key];
$query['name'] = $value;
}
}
}
// Magic
if ( isset( $query['pagename'] ) ) {
$query['pagename'] .= '__trashed' ;
}
if ( isset( $query['name'] ) ) {
$query['name'] .= '__trashed' ;
}
$query['post_status'] = array( 'trash' );
// Resolve conflicts between posts with numeric slugs and date archive queries.
$query = wp_resolve_numeric_slug_conflicts( $query );
// Do the query
$query = new WP_Query( $query );
if ( $query->found_posts == 1 ) {
return true;
} else {
return false;
}
}
}
return false;
}
/**
* create the menu entry
*/
function admin_menu() {
$this->admin_handle = add_theme_page ( __( '404 Error Page', "404page" ), __( '404 Error Page', '404page' ), 'manage_options', '404pagesettings', array( $this, 'admin_page' ) );
}
/**
* add admin css file
*/
function admin_css() {
if ( get_current_screen()->id == $this->admin_handle ) {
wp_enqueue_style( '404pagecss', plugins_url( 'assets/css/404page-ui.css', $this->_file ) );
}
}
/**
* add admin js files
*/
function admin_js() {
wp_enqueue_script( '404pagejs', plugins_url( 'assets/js/404page.js', $this->_file ), 'jquery', $this->version, true );
if ( get_current_screen()->id == $this->admin_handle ) {
wp_enqueue_script( '404page-ui', plugins_url( 'assets/js/404page-ui.js', $this->_file ), 'jquery', $this->version, true );
}
}
/**
* show admin page
*/
function admin_page() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
plugin_name; ?>
post_status != 'publish' ) {
$pageid = -1;
}
}
return $pageid;
}
/**
* get the selected method
*/
private function get_404page_method() {
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
// WPML is active
return 'CMP';
} else {
return get_option( '404page_method', 'STD' );
}
}
/**
* do we have to hide the selected 404 page from the page list?
*/
private function get_404page_hide() {
return (bool)get_option( '404page_hide', false );
}
/**
* do we have to fire an 404 error if the selected page is accessed directly?
*/
private function get_404page_fire_error() {
return (bool)get_option( '404page_fire_error', true );
}
/**
* do we have to force the 404 error after loading the page?
*/
private function get_404page_force_error() {
return (bool)get_option( '404page_force_error', false );
}
/**
* do we have to disable the URL guessing?
*/
private function get_404page_no_url_guessing() {
return (bool)get_option( '404page_no_url_guessing', false );
}
/**
* do we have to send an http 410 error in case the object is in trash?
* @since 3.2
*/
private function get_404page_http410_if_trashed() {
return (bool)get_option( '404page_http410_if_trashed', false );
}
/**
* get the id of the 404 page in the current language if WPML or Polylang is active
*/
private function get_page_id() {
$pageid = $this->settings['404page_page_id'];
if ( $pageid > 0 ) {
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
// WPML is active
$pageid = apply_filters( 'wpml_object_id', $pageid, 'page', true );
} elseif ( defined( 'POLYLANG_VERSION' ) ) {
// Polylang is active
$translatedpageid = pll_get_post( $pageid );
if ( !empty( $translatedpageid ) && 'publish' == get_post_status( $translatedpageid ) ) {
$pageid = $translatedpageid;
}
}
}
return $pageid;
}
/**
* get 404 pages in all available languages
* if WPML is active this function returns an array of all page ids in all available languages
* otherwise it returns the page id as array
* introduced in v 2.4
*/
private function get_all_page_ids() {
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
// WPML is active
// get an array for all translations
$pageid = $this->settings['404page_page_id'];
$pages = array( $pageid );
if ( $pageid > 0 ) {
$languages = apply_filters( 'wpml_active_languages', NULL );
if ( !empty( $languages ) ) {
foreach( $languages as $l ) {
$p = apply_filters( 'wpml_object_id', $pageid, 'page', false, $l['language_code'] );
if ( $p ) {
$pages[] = $p;
}
}
}
}
$pageids = array_unique( $pages, SORT_NUMERIC );
} else {
$pageids = array( $this->get_page_id() );
}
return $pageids;
}
/**
* fire 404page_after_404 hook to make plugin expandable
*/
function do_404page_action() {
do_action( '404page_after_404' );
}
/**
* add links to plugins table
*/
function add_settings_links( $links ) {
return array_merge( $links, array( '' . __( 'Settings', '404page' ) . '', '' . __( 'Please rate plugin', '404page' ) . '' ) );
}
/**
* uninstall plugin
*/
function uninstall() {
if( is_multisite() ) {
$this->uninstall_network();
} else {
$this->uninstall_single();
}
}
/**
* uninstall network wide
*/
function uninstall_network() {
global $wpdb;
$activeblog = $wpdb->blogid;
$blogids = $wpdb->get_col( esc_sql( 'SELECT blog_id FROM ' . $wpdb->blogs ) );
foreach ($blogids as $blogid) {
switch_to_blog( $blogid );
$this->uninstall_single();
}
switch_to_blog( $activeblog );
}
/**
* uninstall for a single blog
*/
function uninstall_single() {
foreach ( $this->settings as $key => $value) {
delete_option( $key );
}
}
/**
* functions for theme usage
*/
// check if there's a custom 404 page set
function pp_404_is_active() {
return ( $this->settings['404page_page_id'] > 0 );
}
// activate the native theme support
function pp_404_set_native_support() {
$this->settings['404page_native'] = true;
}
// get the title - native theme support
function pp_404_get_the_title() {
$title = '';
if ( $this->settings['404page_page_id'] > 0 && $this->settings['404page_native'] ) {
$title = get_the_title( $this->get_page_id() );
}
return $title;
}
// print title - native theme support
function pp_404_the_title() {
echo $this->pp_404_get_the_title();
}
// get the content - native theme support
function pp_404_get_the_content() {
$content = '';
if ( $this->settings['404page_page_id'] > 0 && $this->settings['404page_native'] ) {
$content = apply_filters( 'the_content', get_post_field( 'post_content', $this->get_page_id() ) );
}
return $content;
}
// print content - native theme support
function pp_404_the_content() {
echo $this->pp_404_get_the_content();
}
}
}
?>