| @@ -1,187 +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_Factory401_Plugin { | |
| 17 | + class WINP_Plugin extends Wbcr_Factory419_Plugin { | |
| 17 | 18 | |
| 18 | - /** | |
| 19 | - * @var Wbcr_Factory401_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 | } |
| 42 | - } | |
| 43 | 45 | |
| 44 | - /** | |
| 45 | - * @return Wbcr_Factory401_Plugin | |
| 46 | - */ | |
| 47 | - public static function app() | |
| 48 | - { | |
| 49 | - return self::$app; | |
| 46 | + $this->load_backend(); | |
| 50 | 47 | } |
| 48 | + } | |
| 51 | 49 | |
| 52 | - /** | |
| 53 | - * @return bool | |
| 54 | - */ | |
| 55 | - public function currentUserCan() | |
| 56 | - { | |
| 57 | - return current_user_can('manage_options'); | |
| 58 | - } | |
| 50 | + /** | |
| 51 | + * @return WINP_Plugin | |
| 52 | + */ | |
| 53 | + public static function app() { | |
| 54 | + return self::$app; | |
| 55 | + } | |
| 59 | 56 | |
| 60 | - protected function setTextDomain() | |
| 61 | - { | |
| 57 | + /** | |
| 58 | + * @return bool | |
| 59 | + */ | |
| 60 | + public function currentUserCan() { | |
| 61 | + return current_user_can( 'manage_options' ); | |
| 62 | + } | |
| 62 | 63 | |
| 63 | - load_plugin_textdomain('insert-php', false, dirname(WINP_PLUGIN_BASE) . '/languages/'); | |
| 64 | - } | |
| 65 | - | |
| 66 | - protected function setModules() | |
| 67 | - { | |
| 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' ); | |
| 68 | 71 | |
| 69 | - $this->load(array( | |
| 70 | - array('libs/factory/bootstrap', 'factory_bootstrap_401', 'admin'), | |
| 71 | - array('libs/factory/forms', 'factory_forms_402', 'admin'), | |
| 72 | - array('libs/factory/pages', 'factory_pages_402', 'admin'), | |
| 73 | - array('libs/factory/types', 'factory_types_401'), | |
| 74 | - array('libs/factory/metaboxes', 'factory_metaboxes_400', 'admin'), | |
| 75 | - array('libs/factory/viewtables', 'factory_viewtables_401', 'admin'), | |
| 76 | - array('libs/factory/shortcodes', 'factory_shortcodes_321', 'all'), | |
| 77 | - array('libs/factory/notices', 'factory_notices_401', 'admin') | |
| 78 | - )); | |
| 79 | - } | |
| 72 | + return new WINP_Execute_Snippet(); | |
| 73 | + } | |
| 80 | 74 | |
| 81 | - private function registerPages() | |
| 82 | - { | |
| 83 | - $this->registerPage('WINP_SettingsPage', WINP_PLUGIN_DIR . '/admin/pages/settings.php'); | |
| 84 | - } | |
| 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' ); | |
| 85 | 82 | |
| 86 | - private function registerTypes() | |
| 87 | - { | |
| 88 | - $this->registerType('WSC_TasksItemType', WINP_PLUGIN_DIR . '/admin/types/snippets-post-types.php'); | |
| 89 | - } | |
| 83 | + return new WINP_Api(); | |
| 84 | + } | |
| 90 | 85 | |
| 91 | - private function registerShortcodes() | |
| 92 | - { | |
| 93 | - require_once(WINP_PLUGIN_DIR . '/includes/shortcodes.php'); | |
| 94 | - Wbcr_FactoryShortcodes321::register('WINP_SnippetShortcode', $this); | |
| 95 | - } | |
| 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' ); | |
| 96 | 93 | |
| 97 | - private function adminScripts() | |
| 98 | - { | |
| 99 | - require_once(WINP_PLUGIN_DIR . '/admin/includes/class.snippets.viewtable.php'); | |
| 100 | - require_once(WINP_PLUGIN_DIR . '/admin/boot.php'); | |
| 94 | + return new WINP_Common_Snippet(); | |
| 95 | + } | |
| 101 | 96 | |
| 102 | - $this->registerTypes(); | |
| 103 | - $this->registerPages(); | |
| 104 | - } | |
| 105 | - | |
| 106 | - private function globalScripts() | |
| 107 | - { | |
| 108 | - $this->registerShortcodes(); | |
| 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 | + }*/ | |
| 109 | 105 | |
| 110 | - add_action('plugins_loaded', array($this, 'executeActiveSnippets'), 1); | |
| 111 | - } | |
| 106 | + protected function initActivation() { | |
| 107 | + include_once( WINP_PLUGIN_DIR . '/admin/activation.php' ); | |
| 108 | + $this->registerActivation( 'WINP_Activation' ); | |
| 109 | + } | |
| 112 | 110 | |
| 113 | - /** | |
| 114 | - * Execute the snippets once the plugins are loaded | |
| 115 | - */ | |
| 116 | - public function executeActiveSnippets() | |
| 117 | - { | |
| 118 | - global $wpdb; | |
| 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' ); | |
| 119 | 118 | |
| 120 | - $save_mode = false; | |
| 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 | + } | |
| 121 | 125 | |
| 122 | - if( isset($_REQUEST['wbcr-php-snippets-safe-mode']) && !isset($_COOKIE['wbcr-php-snippets-safe-mode']) && $this->currentUserCan() ) { | |
| 123 | - $save_mode = true; | |
| 124 | - setcookie("wbcr-php-snippets-safe-mode", 1, time() + 3600); | |
| 125 | - } | |
| 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' ); | |
| 126 | 133 | |
| 127 | - $snippets = $wpdb->get_results("SELECT {$wpdb->posts}.ID FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id ) WHERE ( | |
| 128 | - ( {$wpdb->postmeta}.meta_key = '" . $this->getPrefix() . "snippet_scope' AND {$wpdb->postmeta}.meta_value = 'evrywhere' ) | |
| 129 | -) AND {$wpdb->posts}.post_type = '" . WINP_SNIPPETS_POST_TYPE . "' AND (({$wpdb->posts}.post_status = 'publish'))"); | |
| 134 | + if ( ! ( defined( 'WASP_PLUGIN_ACTIVE' ) && WASP_PLUGIN_ACTIVE ) ) { | |
| 135 | + $this->registerPage( 'WINP_ImportPage', WINP_PLUGIN_DIR . '/admin/pages/import.php' ); | |
| 136 | + } | |
| 137 | + } | |
| 130 | 138 | |
| 131 | - if( empty($snippets) ) { | |
| 132 | - return; | |
| 133 | - } | |
| 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 ); | |
| 134 | 147 | |
| 135 | - foreach((array)$snippets as $snippet) { | |
| 136 | - $id = (int)$snippet->ID; | |
| 148 | + require_once( WINP_PLUGIN_DIR . '/admin/types/snippets-taxonomy.php' ); | |
| 149 | + Wbcr_FactoryTaxonomies330::register( 'WINP_SnippetsTaxonomy', $this ); | |
| 150 | + } | |
| 137 | 151 | |
| 138 | - $is_active = (int)WINP_Helper::getMetaOption($id, 'snippet_activate', 0); | |
| 139 | - $code = WINP_Helper::getMetaOption($id, 'snippet_code'); | |
| 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(); | |
| 140 | 159 | |
| 141 | - if( $is_active ) { | |
| 142 | - if( isset($_POST['wbcr_inp_snippet_scope']) && isset($_POST['post_ID']) && (int)$_POST['post_ID'] === $id && $this->currentUserCan() ) { | |
| 143 | - return; | |
| 144 | - } | |
| 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' ); | |
| 165 | + } | |
| 145 | 166 | |
| 146 | - if( ($save_mode || isset($_COOKIE['wbcr-php-snippets-safe-mode'])) && $this->currentUserCan() ) { | |
| 147 | - return; | |
| 148 | - } | |
| 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' ); | |
| 149 | 174 | |
| 150 | - $this->executeSnippet($code, $id); | |
| 151 | - } | |
| 152 | - } | |
| 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 ); | |
| 153 | 181 | } |
| 182 | + } | |
| 154 | 183 | |
| 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(); | |
| 193 | + | |
| 194 | + $this->getExecuteObject()->registerHooks(); | |
| 195 | + $this->register_shortcodes(); | |
| 196 | + | |
| 155 | 197 | /** |
| 156 | - * Execute a snippet | |
| 157 | - * | |
| 158 | - * Code must NOT be escaped, as | |
| 159 | - * it will be executed directly | |
| 160 | - * | |
| 161 | - * @param string $code The snippet code to execute | |
| 162 | - * @param int $id The snippet ID | |
| 163 | - * @param bool $catch_output Whether to attempt to suppress the output of execution using buffers | |
| 164 | - * | |
| 165 | - * @return mixed The result of the code execution | |
| 198 | + * Enables/Disable safe mode, in which the php code will not be executed. | |
| 166 | 199 | */ |
| 167 | - public function executeSnippet($code, $id = 0, $catch_output = true) | |
| 168 | - { | |
| 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(); | |
| 205 | + } | |
| 169 | 206 | |
| 170 | - if( empty($code) ) { | |
| 171 | - return false; | |
| 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(); | |
| 172 | 211 | } |
| 212 | + }, - 1 ); | |
| 213 | + } | |
| 173 | 214 | |
| 174 | - if( $catch_output ) { | |
| 175 | - ob_start(); | |
| 176 | - } | |
| 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' ); | |
| 177 | 232 | |
| 178 | - $result = eval($code); | |
| 233 | + $this->get_common_object()->registerHooks(); | |
| 179 | 234 | |
| 180 | - if( $catch_output ) { | |
| 181 | - ob_end_clean(); | |
| 182 | - } | |
| 235 | + $this->register_types(); | |
| 183 | 236 | |
| 184 | - return $result; | |
| 185 | - } | |
| 237 | + new WINP_Filter_List(); | |
| 238 | + new WINP_Export_Snippet(); | |
| 239 | + new WINP_Import_Snippet(); | |
| 240 | + new WINP_WarningNotices(); | |
| 241 | + new WINP_Dashboard_Widget(); | |
| 242 | + | |
| 243 | + # Required for i18n to be loaded properly | |
| 244 | + add_action( 'plugins_loaded', [ $this, 'register_pages' ] ); | |
| 245 | + | |
| 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 ); | |
| 186 | 249 | } |
| 187 | - } | |
| 250 | + } | |
| 251 | +} | |