PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.0.6
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.0.6
2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 2.7.1 All 27 releases
insert-php / includes / class.plugin.php

class.plugin.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.0.6, at includes/class.plugin.php

223 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHP snippets plugin base
4 * @author Webcraftic <wordpress.webraftic@gmail.com>
5 * @copyright (c) 19.02.2018, Webcraftic
6 * @version 1.0
7 */
8
9 // Exit if accessed directly
10 if( !defined('ABSPATH') ) {
11 exit;
12 }
13
14 if( !class_exists('WINP_Plugin') ) {
15
16 class WINP_Plugin extends Wbcr_Factory404_Plugin {
17
18 /**
19 * @var Wbcr_Factory404_Plugin
20 */
21 private static $app;
22
23 /**
24 * @param string $plugin_path
25 * @param array $data
26 * @throws Exception
27 */
28 public function __construct($plugin_path, $data)
29 {
30 parent::__construct($plugin_path, $data);
31
32 self::$app = $this;
33
34 $this->setTextDomain();
35 $this->setModules();
36
37 $this->globalScripts();
38
39 if( is_admin() ) {
40 $this->adminScripts();
41 }
42 }
43
44 /**
45 * @return WINP_Plugin
46 */
47 public static function app()
48 {
49 return self::$app;
50 }
51
52 /**
53 * @return bool
54 */
55 public function currentUserCan()
56 {
57 return current_user_can('manage_options');
58 }
59
60 protected function setTextDomain()
61 {
62
63 load_plugin_textdomain('insert-php', false, dirname(WINP_PLUGIN_BASE) . '/languages/');
64 }
65
66 protected function setModules()
67 {
68
69 $this->load(array(
70 array('libs/factory/bootstrap', 'factory_bootstrap_404', 'admin'),
71 array('libs/factory/forms', 'factory_forms_405', 'admin'),
72 array('libs/factory/pages', 'factory_pages_405', 'admin'),
73 array('libs/factory/types', 'factory_types_404'),
74 array('libs/factory/taxonomies', 'factory_taxonomies_324'),
75 array('libs/factory/metaboxes', 'factory_metaboxes_403', 'admin'),
76 array('libs/factory/viewtables', 'factory_viewtables_403', 'admin'),
77 array('libs/factory/shortcodes', 'factory_shortcodes_324', 'all'),
78 array('libs/factory/notices', 'factory_notices_403', 'admin'),
79
80 ));
81 }
82
83 private function registerPages()
84 {
85 $this->registerPage('WINP_ExportPage', WINP_PLUGIN_DIR . '/admin/pages/export.php');
86 $this->registerPage('WINP_SettingsPage', WINP_PLUGIN_DIR . '/admin/pages/settings.php');
87 }
88
89 private function registerTypes()
90 {
91 $this->registerType('WSC_TasksItemType', WINP_PLUGIN_DIR . '/admin/types/snippets-post-types.php');
92
93 require_once(WINP_PLUGIN_DIR . '/admin/types/snippets-taxonomy.php');
94 Wbcr_FactoryTaxonomies324::register('WINP_SnippetsTaxonomy', $this);
95 }
96
97 private function registerShortcodes()
98 {
99 require_once(WINP_PLUGIN_DIR . '/includes/shortcodes.php');
100 Wbcr_FactoryShortcodes324::register('WINP_SnippetShortcode', $this);
101 }
102
103 private function adminScripts()
104 {
105 require_once(WINP_PLUGIN_DIR . '/admin/includes/class.snippets.viewtable.php');
106 require_once(WINP_PLUGIN_DIR . '/admin/boot.php');
107
108 $this->registerTypes();
109 $this->registerPages();
110 }
111
112 private function globalScripts()
113 {
114 $this->registerShortcodes();
115
116 add_action('plugins_loaded', array($this, 'executeActiveSnippets'), 1);
117 }
118
119 /**
120 * Execute the snippets once the plugins are loaded
121 */
122 public function executeActiveSnippets()
123 {
124 global $wpdb;
125
126 $save_mode = false;
127
128 if( isset($_REQUEST['wbcr-php-snippets-safe-mode']) && !isset($_COOKIE['wbcr-php-snippets-safe-mode']) && $this->currentUserCan() ) {
129 $save_mode = true;
130 setcookie("wbcr-php-snippets-safe-mode", 1, time() + 3600);
131 }
132
133 $snippets = $wpdb->get_results("SELECT {$wpdb->posts}.ID FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id ) WHERE (
134 ( {$wpdb->postmeta}.meta_key = '" . $this->getPrefix() . "snippet_scope' AND {$wpdb->postmeta}.meta_value = 'evrywhere' )
135 ) AND {$wpdb->posts}.post_type = '" . WINP_SNIPPETS_POST_TYPE . "' AND (({$wpdb->posts}.post_status = 'publish'))");
136
137 if( empty($snippets) ) {
138 return;
139 }
140
141 foreach((array)$snippets as $snippet) {
142 $id = (int)$snippet->ID;
143
144 $is_active = (int)WINP_Helper::getMetaOption($id, 'snippet_activate', 0);
145 $code = WINP_Helper::getMetaOption($id, 'snippet_code');
146
147 if( $is_active ) {
148 if( isset($_POST['wbcr_inp_snippet_scope']) && isset($_POST['post_ID']) && (int)$_POST['post_ID'] === $id && $this->currentUserCan() ) {
149 return;
150 }
151
152 if( ($save_mode || isset($_COOKIE['wbcr-php-snippets-safe-mode'])) && $this->currentUserCan() ) {
153 return;
154 }
155
156 $this->executeSnippet($code, $id);
157 }
158 }
159 }
160
161 /**
162 * Execute a snippet
163 *
164 * Code must NOT be escaped, as
165 * it will be executed directly
166 *
167 * @param string $code The snippet code to execute
168 * @param int $id The snippet ID
169 * @param bool $catch_output Whether to attempt to suppress the output of execution using buffers
170 *
171 * @return mixed The result of the code execution
172 */
173 public function executeSnippet($code, $id = 0, $catch_output = true)
174 {
175
176 if( empty($code) ) {
177 return false;
178 }
179
180 if( $catch_output ) {
181 ob_start();
182 }
183
184 $result = eval($code);
185
186 if( $catch_output ) {
187 ob_end_clean();
188 }
189
190 return $result;
191 }
192
193 /**
194 * Retrieve the first error in a snippet's code
195 *
196 * @param int $snippet_id
197 *
198 * @return array|bool
199 */
200 public function getSnippetError($snippet_id)
201 {
202 if( !intval($snippet_id) ) {
203 return false;
204 }
205
206 $snippet_code = WINP_Helper::getMetaOption($snippet_id, 'snippet_code');
207
208 $result = $this->executeSnippet($snippet_code, $snippet_id);
209
210 if( false !== $result ) {
211 return false;
212 }
213
214 $error = error_get_last();
215
216 if( is_null($error) ) {
217 return false;
218 }
219
220 return $error;
221 }
222 }
223 }