PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.2.7
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.2.7
2.7.7 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 All 28 releases
← All changes | includes/class.plugin.php +203 -175 2.0.62.2.7 View file →
@@ -1,223 +1,251 @@
1 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 - */
2 +/**
3 + * PHP snippets plugin base
4 + *
5 + * @author Webcraftic <wordpress.webraftic@gmail.com>
6 + * @copyright (c) 19.02.2018, Webcraftic
7 + * @version 1.0
8 + */
8 9
9 - // Exit if accessed directly
10 - if( !defined('ABSPATH') ) {
11 - exit;
12 - }
10 +// Exit if accessed directly
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit;
13 +}
13 14
14 - if( !class_exists('WINP_Plugin') ) {
15 +if ( ! class_exists( 'WINP_Plugin' ) ) {
15 16
16 - class WINP_Plugin extends Wbcr_Factory404_Plugin {
17 + class WINP_Plugin extends Wbcr_Factory419_Plugin {
17 18
18 - /**
19 - * @var Wbcr_Factory404_Plugin
20 - */
21 - private static $app;
19 + /**
20 + * @var Wbcr_Factory419_Plugin
21 + */
22 + private static $app;
22 23
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);
24 + /**
25 + * @param string $plugin_path
26 + * @param array $data
27 + *
28 + * @throws Exception
29 + */
30 + public function __construct( $plugin_path, $data ) {
31 + parent::__construct( $plugin_path, $data );
31 32
32 - self::$app = $this;
33 + self::$app = $this;
33 34
34 - $this->setTextDomain();
35 - $this->setModules();
35 + $this->load_global();
36 36
37 - $this->globalScripts();
37 + if ( is_admin() ) {
38 + $this->initActivation();
38 39
39 - if( is_admin() ) {
40 - $this->adminScripts();
40 + if ( WINP_Helper::doing_ajax() ) {
41 + require( WINP_PLUGIN_DIR . '/admin/ajax/ajax.php' );
42 + require( WINP_PLUGIN_DIR . '/admin/ajax/check-license.php' );
43 + require( WINP_PLUGIN_DIR . '/admin/ajax/snippet-library.php' );
41 44 }
45 +
46 + $this->load_backend();
42 47 }
48 + }
43 49
44 - /**
45 - * @return WINP_Plugin
46 - */
47 - public static function app()
48 - {
49 - return self::$app;
50 - }
50 + /**
51 + * @return WINP_Plugin
52 + */
53 + public static function app() {
54 + return self::$app;
55 + }
51 56
52 - /**
53 - * @return bool
54 - */
55 - public function currentUserCan()
56 - {
57 - return current_user_can('manage_options');
58 - }
57 + /**
58 + * @return bool
59 + */
60 + public function currentUserCan() {
61 + return current_user_can( 'manage_options' );
62 + }
59 63
60 - protected function setTextDomain()
61 - {
64 + /**
65 + * Get Execute_Snippet object
66 + *
67 + * @return WINP_Execute_Snippet
68 + */
69 + public function getExecuteObject() {
70 + require_once( WINP_PLUGIN_DIR . '/includes/class.execute.snippet.php' );
62 71
63 - load_plugin_textdomain('insert-php', false, dirname(WINP_PLUGIN_BASE) . '/languages/');
64 - }
65 -
66 - protected function setModules()
67 - {
72 + return new WINP_Execute_Snippet();
73 + }
68 74
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'),
75 + /**
76 + * Get WINP_Api object
77 + *
78 + * @return WINP_Api
79 + */
80 + public function get_api_object() {
81 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.api.php' );
79 82
80 - ));
81 - }
83 + return new WINP_Api();
84 + }
82 85
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 - }
86 + /**
87 + * Get WINP_Common_Snippet object
88 + *
89 + * @return WINP_Common_Snippet
90 + */
91 + public function get_common_object() {
92 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.common.snippet.php' );
88 93
89 - private function registerTypes()
90 - {
91 - $this->registerType('WSC_TasksItemType', WINP_PLUGIN_DIR . '/admin/types/snippets-post-types.php');
94 + return new WINP_Common_Snippet();
95 + }
92 96
93 - require_once(WINP_PLUGIN_DIR . '/admin/types/snippets-taxonomy.php');
94 - Wbcr_FactoryTaxonomies324::register('WINP_SnippetsTaxonomy', $this);
95 - }
97 + /**
98 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
99 + * @since 2.2.0
100 + * @throws \Exception
101 + */
102 + /*public function plugins_loaded() {
103 + $this->register_pages();
104 + }*/
96 105
97 - private function registerShortcodes()
98 - {
99 - require_once(WINP_PLUGIN_DIR . '/includes/shortcodes.php');
100 - Wbcr_FactoryShortcodes324::register('WINP_SnippetShortcode', $this);
101 - }
106 + protected function initActivation() {
107 + include_once( WINP_PLUGIN_DIR . '/admin/activation.php' );
108 + $this->registerActivation( 'WINP_Activation' );
109 + }
102 110
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');
111 + /**
112 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
113 + * @since 2.2.0
114 + * @throws \Exception
115 + */
116 + public function register_pages() {
117 + require_once( WINP_PLUGIN_DIR . '/admin/pages/page.php' );
107 118
108 - $this->registerTypes();
109 - $this->registerPages();
110 - }
111 -
112 - private function globalScripts()
113 - {
114 - $this->registerShortcodes();
119 + $this->registerPage( 'WINP_NewItemPage', WINP_PLUGIN_DIR . '/admin/pages/new-item.php' );
120 + $this->registerPage( 'WINP_SettingsPage', WINP_PLUGIN_DIR . '/admin/pages/settings.php' );
121 + $this->registerPage( 'WINP_SnippetLibraryPage', WINP_PLUGIN_DIR . '/admin/pages/snippet-library.php' );
122 + $this->registerPage( 'WINP_License_Page', WINP_PLUGIN_DIR . '/admin/pages/license.php' );
123 + $this->registerPage( 'WINP_AboutPage', WINP_PLUGIN_DIR . '/admin/pages/about.php' );
124 + }
115 125
116 - add_action('plugins_loaded', array($this, 'executeActiveSnippets'), 1);
126 + /**
127 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
128 + * @since 2.2.0
129 + * @throws \Exception
130 + */
131 + public function register_depence_pages() {
132 + require_once( WINP_PLUGIN_DIR . '/admin/pages/page.php' );
133 +
134 + if ( ! ( defined( 'WASP_PLUGIN_ACTIVE' ) && WASP_PLUGIN_ACTIVE ) ) {
135 + $this->registerPage( 'WINP_ImportPage', WINP_PLUGIN_DIR . '/admin/pages/import.php' );
117 136 }
137 + }
118 138
119 - /**
120 - * Execute the snippets once the plugins are loaded
121 - */
122 - public function executeActiveSnippets()
123 - {
124 - global $wpdb;
139 + /**
140 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
141 + * @since 2.2.0
142 + * @throws \Exception
143 + */
144 + private function register_types() {
145 + require_once( WINP_PLUGIN_DIR . '/admin/types/snippets-post-types.php' );
146 + Wbcr_FactoryTypes410::register( 'WINP_SnippetsType', $this );
125 147
126 - $save_mode = false;
148 + require_once( WINP_PLUGIN_DIR . '/admin/types/snippets-taxonomy.php' );
149 + Wbcr_FactoryTaxonomies330::register( 'WINP_SnippetsTaxonomy', $this );
150 + }
127 151
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 - }
152 + /**
153 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
154 + * @since 2.2.0
155 + */
156 + private function register_shortcodes() {
157 + $is_cron = WINP_Helper::doing_cron();
158 + $is_rest = WINP_Helper::doing_rest_api();
132 159
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;
160 + $action = WINP_Plugin::app()->request->get( 'action', '' );
161 + if ( ! ( 'edit' == $action && is_admin() ) && ! $is_cron && ! $is_rest ) {
162 + if ( WINP_Plugin::app()->getOption( 'support_old_shortcodes' ) ) {
163 + // todo: Deprecated
164 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-insert-php.php' );
139 165 }
140 166
141 - foreach((array)$snippets as $snippet) {
142 - $id = (int)$snippet->ID;
167 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcodes.php' );
168 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-php.php' );
169 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-text.php' );
170 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-universal.php' );
171 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-css.php' );
172 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-js.php' );
173 + require_once( WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-html.php' );
143 174
144 - $is_active = (int)WINP_Helper::getMetaOption($id, 'snippet_activate', 0);
145 - $code = WINP_Helper::getMetaOption($id, 'snippet_code');
175 + WINP_Helper::register_shortcode( 'WINP_SnippetShortcodePhp', $this );
176 + WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeText', $this );
177 + WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeUniversal', $this );
178 + WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeCss', $this );
179 + WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeJs', $this );
180 + WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeHtml', $this );
181 + }
182 + }
146 183
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 - }
184 + /**
185 + * Initialization and require files for backend and frontend.
186 + *
187 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
188 + * @since 2.2.0
189 + */
190 + private function load_global() {
191 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.gutenberg.snippet.php' );
192 + new WINP_Gutenberg_Snippet();
151 193
152 - if( ($save_mode || isset($_COOKIE['wbcr-php-snippets-safe-mode'])) && $this->currentUserCan() ) {
153 - return;
154 - }
194 + $this->getExecuteObject()->registerHooks();
195 + $this->register_shortcodes();
155 196
156 - $this->executeSnippet($code, $id);
157 - }
158 - }
159 - }
160 -
161 197 /**
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
198 + * Enables/Disable safe mode, in which the php code will not be executed.
172 199 */
173 - public function executeSnippet($code, $id = 0, $catch_output = true)
174 - {
175 -
176 - if( empty($code) ) {
177 - return false;
200 + add_action( 'plugins_loaded', function () {
201 + if ( isset( $_GET['wbcr-php-snippets-safe-mode'] ) ) {
202 + WINP_Helper::enable_safe_mode();
203 + wp_safe_redirect( remove_query_arg( [ 'wbcr-php-snippets-safe-mode' ] ) );
204 + die();
178 205 }
179 206
180 - if( $catch_output ) {
181 - ob_start();
207 + if ( isset( $_GET['wbcr-php-snippets-disable-safe-mode'] ) ) {
208 + WINP_Helper::disable_safe_mode();
209 + wp_safe_redirect( remove_query_arg( [ 'wbcr-php-snippets-disable-safe-mode' ] ) );
210 + die();
182 211 }
212 + }, - 1 );
213 + }
183 214
184 - $result = eval($code);
215 + /**
216 + * Initialization and require files for backend.
217 + *
218 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
219 + * @since 2.2.0
220 + * @throws \Exception
221 + */
222 + private function load_backend() {
223 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.snippets.viewtable.php' );
224 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.filter.snippet.php' );
225 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.actions.snippet.php' );
226 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.import.snippet.php' );
227 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.notices.php' );
228 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.request.php' );
229 + require_once( WINP_PLUGIN_DIR . '/admin/includes/class.dashboard.widget.php' );
230 + require_once( WINP_PLUGIN_DIR . '/admin/metaboxes/metabox.php' );
231 + require_once( WINP_PLUGIN_DIR . '/admin/boot.php' );
185 232
186 - if( $catch_output ) {
187 - ob_end_clean();
188 - }
233 + $this->get_common_object()->registerHooks();
189 234
190 - return $result;
191 - }
235 + $this->register_types();
192 236
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 - }
237 + new WINP_Filter_List();
238 + new WINP_Export_Snippet();
239 + new WINP_Import_Snippet();
240 + new WINP_WarningNotices();
241 + new WINP_Dashboard_Widget();
205 242
206 - $snippet_code = WINP_Helper::getMetaOption($snippet_id, 'snippet_code');
243 + # Required for i18n to be loaded properly
244 + add_action( 'plugins_loaded', [ $this, 'register_pages' ] );
207 245
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 - }
246 + # Required for compatibility with the premium plugin.
247 + # We set the priority to 30 to wait for the premium plugin to load.
248 + add_action( 'plugins_loaded', [ $this, 'register_depence_pages' ], 30 );
222 249 }
223 - }
250 + }
251 +}