forked from tommcfarlin/page-template-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
207 lines (158 loc) · 6.31 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
<?php
/*
Plugin Name: Page Template Plugin
Plugin URI: http://github.com/tommcfarlin/page-template-plugin/
Description: An example plugin used to show how to include page templates with your plugin.
Version: 0.1
Author: Tom McFarlin
Author URI: http://tommcfarlin.com/
Author Email: [email protected]
License:
Copyright 2013 Tom McFarlin ([email protected])
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.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if( ! defined( 'PAGE_TEMPLATE_PLUGIN' ) ) {
define( 'PAGE_TEMPLATE_PLUGIN', '0.1' );
} // end if
/**
* @package Page Template Example
* @version 0.1
* @since 0.1
*/
class Page_Template_Plugin {
/*--------------------------------------------*
* Attributes
*--------------------------------------------*/
/** A reference to an instance of this class. **/
private static $instance;
/*--------------------------------------------*
* Constructor
*--------------------------------------------*/
/**
* Returns an instance of this class. This is the Singleton design pattern.
*
* @return OBJECT A reference to an instance of this class.
*/
public static function getInstance() {
if( null == self::$instance ) {
self::$instance = new Page_Template_Plugin();
} // end if
return self::$instance;
} // end getInstance
/**
* Initializes the plugin by setting localization, filters, and administration functions.
*
* @version 1.0
* @since 1.0
*/
private function __construct() {
add_action( 'init', array( $this, 'plugin_textdomain' ) );
register_activation_hook( __FILE__, array( $this, 'register_project_template' ) );
register_deactivation_hook( __FILE__, array( $this, 'deregister_project_template' ) );
} // end constructor
/*--------------------------------------------*
* Localization
*--------------------------------------------*/
/**
* Loads the plugin text domain for translation
*
* @version 1.0
* @since 1.0
*/
public function plugin_textdomain() {
load_plugin_textdomain( 'pte', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
} // end plugin_textdomain
/*--------------------------------------------*
* Activation and Deactivation Hooks
*--------------------------------------------*/
/**
* Copies the template from the `views/templates` directory to the root of the active theme
* directory so that it can be applied to pages.
*
* @verison 1.0
* @since 1.0
*/
public function register_project_template() {
// Get the template source and destination for copying from the plugin to the theme directory
$template_destination = $this->get_template_destination();
$template_source = $this->get_template_source();
// Now actually copy the template file from the plugin to the destination
$this->copy_page_template( $template_source, $template_destination );
} // end register_project_template
/**
* Removes the template from the theme directory that was added during theme activation.
*
* @version 1.0
* since 1.0
*/
public function deregister_project_template() {
// Get the path to the theme
$theme_dir = get_template_directory();
$template_path = $theme_dir . '/template-example.php';
// If the template file is in the theme path, delete it.
if( file_exists( $template_path ) ) {
unlink( $template_path );
} // end if
} // end deregister_project_template
/*--------------------------------------------*
* Helper Methods
*--------------------------------------------*/
/**
* @return string The destination to the plugin directory relative to the currently active theme
*/
private function get_template_destination() {
return get_template_directory() . '/template-example.php';
} // end get_template_destination
/**
* @return string The path to the template file relative to the plugin.
*/
private function get_template_source() {
return dirname( __FILE__ ) . '/templates/template-example.php';
} // end get_template_source
/**
* Copies the contents of the template from the source file in the plugin
* to the destination in the current theme directory.
*
* This works by first creating an empty file, reading the contents of the template file
* then writing it into the empty file in the theme directory.
*
* Note that this version is for demonstration purposes only and does not include proper
* exception handling for when file operations fail.
*
* @param string $template_source The path on which the template resides.
* @param string $template_destination The path to which the template should be written.
*/
private function copy_page_template( $template_source, $template_destination ) {
// After that, check to see if the template already exists. If so don't copy it; otherwise, copy if
if( ! file_exists( $template_destination ) ) {
// Create an empty version of the file
touch( $template_destination );
// Read the source file starting from the beginning of the file
if( null != ( $template_handle = @fopen( $template_source, 'r' ) ) ) {
// Now read the contents of the file into a string. Read up to the length of the source file
if( null != ( $template_content = fread( $template_handle, filesize( $template_source ) ) ) ) {
// Relinquish the resource
fclose( $template_handle );
} // end if
} // end if
// Now open the file for reading and writing
if( null != ( $template_handle = @fopen( $template_destination, 'r+' ) ) ) {
// Attempt to write the contents of the string
if( null != fwrite( $template_handle, $template_content, strlen( $template_content ) ) ) {
// Relinquish the resource
fclose( $template_handle );
} // end if
} // end if
} // end if
} // end copy_page_template
} // end class
$GLOBALS['ptb'] = Page_Template_Plugin::getInstance();