| 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_Factory401_Plugin { |
| 17 |
|
| 18 |
/** |
| 19 |
* @var Wbcr_Factory401_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 Wbcr_Factory401_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_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 |
} |
| 80 |
|
| 81 |
private function registerPages() |
| 82 |
{ |
| 83 |
$this->registerPage('WINP_SettingsPage', WINP_PLUGIN_DIR . '/admin/pages/settings.php'); |
| 84 |
} |
| 85 |
|
| 86 |
private function registerTypes() |
| 87 |
{ |
| 88 |
$this->registerType('WSC_TasksItemType', WINP_PLUGIN_DIR . '/admin/types/snippets-post-types.php'); |
| 89 |
} |
| 90 |
|
| 91 |
private function registerShortcodes() |
| 92 |
{ |
| 93 |
require_once(WINP_PLUGIN_DIR . '/includes/shortcodes.php'); |
| 94 |
Wbcr_FactoryShortcodes321::register('WINP_SnippetShortcode', $this); |
| 95 |
} |
| 96 |
|
| 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'); |
| 101 |
|
| 102 |
$this->registerTypes(); |
| 103 |
$this->registerPages(); |
| 104 |
} |
| 105 |
|
| 106 |
private function globalScripts() |
| 107 |
{ |
| 108 |
$this->registerShortcodes(); |
| 109 |
|
| 110 |
add_action('plugins_loaded', array($this, 'executeActiveSnippets'), 1); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Execute the snippets once the plugins are loaded |
| 115 |
*/ |
| 116 |
public function executeActiveSnippets() |
| 117 |
{ |
| 118 |
global $wpdb; |
| 119 |
|
| 120 |
$save_mode = false; |
| 121 |
|
| 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 |
$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'))"); |
| 130 |
|
| 131 |
if( empty($snippets) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
foreach((array)$snippets as $snippet) { |
| 136 |
$id = (int)$snippet->ID; |
| 137 |
|
| 138 |
$is_active = (int)WINP_Helper::getMetaOption($id, 'snippet_activate', 0); |
| 139 |
$code = WINP_Helper::getMetaOption($id, 'snippet_code'); |
| 140 |
|
| 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 |
} |
| 145 |
|
| 146 |
if( ($save_mode || isset($_COOKIE['wbcr-php-snippets-safe-mode'])) && $this->currentUserCan() ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
$this->executeSnippet($code, $id); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 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 |
| 166 |
*/ |
| 167 |
public function executeSnippet($code, $id = 0, $catch_output = true) |
| 168 |
{ |
| 169 |
|
| 170 |
if( empty($code) ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
if( $catch_output ) { |
| 175 |
ob_start(); |
| 176 |
} |
| 177 |
|
| 178 |
$result = eval($code); |
| 179 |
|
| 180 |
if( $catch_output ) { |
| 181 |
ob_end_clean(); |
| 182 |
} |
| 183 |
|
| 184 |
return $result; |
| 185 |
} |
| 186 |
} |
| 187 |
} |