forked from billerickson/Genesis-404-Page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
238 lines (189 loc) · 6.1 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* Plugin Name: Genesis 404
* Plugin URI: https://github.com/billerickson/Genesis-404-Plugin
* Description: Customize the content of your 404 page.
* Version: 1.1ms
* Author: Bill Erickson
* Author URI: http://www.billerickson.net
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
class BE_Genesis_404 {
var $instance;
/**
* Construct
*
* Registers our activation hook and init hook
*
* @since 1.0
* @author Bill Erickson
*/
function __construct() {
$this->instance =& $this;
register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
add_action( 'init', array( $this, 'init' ) );
}
/**
* Activation Hook
*
* Confirm site is using Genesis
*
* @since 1.0
* @author Bill Erickson
*/
function activation_hook() {
if ( 'genesis' != basename( TEMPLATEPATH ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf( __( 'Sorry, you can’t activate unless you have installed <a href="%s">Genesis</a>', 'genesis-title-toggle'), 'http://www.billerickson.net/get-genesis' ) );
}
}
/**
* Init
*
* Register all our functions to the appropriate hook
*
* @since 1.0
* @author Bill Erickson
*/
function init() {
// Translations
load_plugin_textdomain( 'genesis-404', false, basename( dirname( __FILE__ ) ) . '/languages' );
// Check to see if should be used
add_action( 'genesis_meta', array( $this, 'maybe_custom_404' ) );
// Search Shortcode
add_shortcode( 'genesis-404-search', array( $this, 'search_shortcode' ) );
}
/**
* Check if custom 404 should be used
*
* @since 1.0
* @author Bill Erickson
*/
function maybe_custom_404() {
if( ( is_404() && is_multisite() ) || ( is_404() && genesis_get_option( 'content', 'genesis-404' ) ) ) {
remove_action( 'genesis_loop', 'genesis_404' );
add_action( 'genesis_loop', array( $this, 'be_genesis_404_loop' ) );
}
}
/**
* Genesis 404 Loop
*
* @since 1.0
* @author Bill Erickson
*/
function be_genesis_404_loop() {
if( is_multisite() && get_current_blog_id() != SITE_ID_CURRENT_SITE )
switch_to_blog( SITE_ID_CURRENT_SITE );
$title = esc_attr( genesis_get_option( 'title', 'genesis-404' ) );
$content = genesis_get_option( 'content', 'genesis-404' );
echo '<div class="post hentry">';
if (!empty( $title ) )
echo '<h1 class="entry-title">' . $title . '</h1>';
if( !empty( $content ) )
echo '<div class="entry-content">' . apply_filters( 'the_content', $content ) . '</div>';
echo '</div>';
if ( is_multisite() )
restore_current_blog();
}
/**
* Search Shortcode
*
* @since 1.1
* @author Bill Erickson
*/
function search_shortcode() {
return '<div class="genesis-404-search">' . get_search_form( false ) . '</div>';
}
}
new BE_Genesis_404;
/**
* Registers a new admin page, providing content and corresponding menu item
* for the Child Theme Settings page.
*
* @since 1.0
* @author Bill Erickson
*/
function be_register_genesis_404_settings() {
class BE_Genesis_404_Settings extends Genesis_Admin_Boxes {
/**
* Create an admin menu item and settings page.
* @since 1.0.0
*/
function __construct() {
// Specify a unique page ID.
$page_id = 'genesis-404';
// Set it as a child to genesis, and define the menu and page titles
$menu_ops = array(
'submenu' => array(
'parent_slug' => 'genesis',
'page_title' => __( 'Genesis - 404 Page', 'genesis-404' ),
'menu_title' => __( '404 Page', 'genesis-404' ),
)
);
// Set up page options. These are optional, so only uncomment if you want to change the defaults
$page_ops = array(
// 'screen_icon' => 'options-general',
// 'save_button_text' => 'Save Settings',
// 'reset_button_text' => 'Reset Settings',
// 'save_notice_text' => 'Settings saved.',
// 'reset_notice_text' => 'Settings reset.',
);
// Give it a unique settings field.
// You'll access them from genesis_get_option( 'option_name', 'child-settings' );
$settings_field = 'genesis-404';
// Set the default values
$default_settings = array(
'title' => '',
'content' => '',
);
// Create the Admin Page
$this->create( $page_id, $menu_ops, $page_ops, $settings_field, $default_settings );
// Initialize the Sanitization Filter
add_action( 'genesis_settings_sanitizer_init', array( $this, 'sanitization_filters' ) );
}
/**
* Set up Sanitization Filters
* @since 1.0.0
*
* See /lib/classes/sanitization.php for all available filters.
*/
function sanitization_filters() {
genesis_add_option_filter( 'no_html', $this->settings_field,
array(
'title',
) );
genesis_add_option_filter( 'safe_html', $this->settings_field,
array(
'content',
) );
}
/**
* Register metaboxes on Child Theme Settings page
* @since 1.0.0
*/
function metaboxes() {
add_meta_box('metabox_404', __( '404 Page', 'genesis-404' ), array( $this, 'metabox_404' ), $this->pagehook, 'main', 'high');
}
/**
* 404 Metabox
* @since 1.0.0
*/
function metabox_404() {
echo '<p>' . __( 'Page Title', 'genesis-404' ) . '<br />
<input type="text" name="' . $this->get_field_name( 'title' ) . '" id="' . $this->get_field_id( 'title' ) . '" value="' . esc_attr( $this->get_field_value( 'title' ) ) . '" size="27" /></p>';
echo '<p>' . __( 'Page Content', 'genesis-404' ) . '</p>';
wp_editor( genesis_get_option( 'content', 'genesis-404' ), $this->get_field_id( 'content' ) );
}
}
global $_be_genesis_404_settings;
$_be_genesis_404_settings = new BE_Genesis_404_Settings;
}
if( !is_multisite() || get_current_blog_id() == SITE_ID_CURRENT_SITE )
add_action( 'genesis_admin_menu', 'be_register_genesis_404_settings' );