/
plugin.php
121 lines (96 loc) · 2.61 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
<?php
/*
Plugin Name: My Plugin
Description: A Plugin based on the GetSimple Plugin Template
Version: 1.0
Author: RJ Zaworski <rj@rjzaworski.com>
Author URI: http://rjzaworski.com/
*/
if (!class_exists('Plugin')):
require_once('plugin/get_simple_plugin.php');
class Plugin extends GetSimplePlugin {
protected
/**
* The very simplest plugins will still need to specify
* a few fields in the `$_info` array.
*/
$_defaults = array(
'email' => 'mailbag@example.com'
),
$_actions = array(
'theme-sidebar' => 'admin_menu'
),
$_filters = array(
'content' => 'filter_content'
);
/**
* Assign the correct plugin ID and call the parent constructor
* @constructor
*/
public function __construct () {
// configure this plugin
$this->_info = array(
'id' => basename(__FILE__, '.php'),
'name' => 'Contact Form',
'version' => '1.0',
'author' => 'RJ Zaworski <rj@rjzaworski.com>',
'author_website' => 'http://rjzaworski.com/',
'description' => 'A Contact Link',
'page_type' => 'theme',
'menu_callback' => 'admin_view'
);
// initiate the plugin
parent::__construct();
}
/**
* Callback attached to `theme-sidebar`: create a menu entry
* @callback
*/
public function admin_menu () {
createSideMenu($this->_info['id'], 'Contact Link');
}
/**
* Callback attached in `admin_menu`: admin settings
* @callback
*/
public function admin_view () {
$data = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$action = $_POST['_action'];
$data['action'] = $action;
// Obligatory CSRF check
if (check_nonce($_POST['_nonce'], $action, $this->_info['id'])) {
// Take action!
switch ($action) {
case 'save':
if ($this->_save($_POST)) {
$data['updated'] = 'Settings saved';
} else {
$data['error'] = 'There was a problem saving data';
}
break;
}
} elseif ($action) {
// Failed CSRF test
$data['error'] = 'Request timed out';
}
}
$this->_load_view('admin', $data);
}
/**
* Callback attached to `content` filter: replace contact tag with content
* @param string $content to filter
* @param string filtered content
*/
public function filter_content ($content) {
$key = '(% contact %)';
if (strpos($content, $key) === FALSE) {
return $content;
}
$email = $this->_settings['email'];
$mailto = "<a href=\"mailto:$email\">$email</a>";
return str_replace($content, $key, $contact_form);
}
}
new Plugin();
endif;