| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP snippets plugin base |
| 4 |
* |
| 5 |
* @package Woody_Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WINP_Plugin' ) ) { |
| 14 |
|
| 15 |
class WINP_Plugin { |
| 16 |
|
| 17 |
/** |
| 18 |
* Custom license provider (overrides parent's premium property) |
| 19 |
* |
| 20 |
* @var WINP_License |
| 21 |
*/ |
| 22 |
public $premium; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var WINP_Plugin |
| 26 |
*/ |
| 27 |
private static $app; |
| 28 |
|
| 29 |
/** |
| 30 |
* Snippets custom post type instance. |
| 31 |
* |
| 32 |
* @var WINP_SnippetsType |
| 33 |
*/ |
| 34 |
private $snippets_type; |
| 35 |
|
| 36 |
/** |
| 37 |
* @throws Exception |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
self::$app = $this; |
| 41 |
|
| 42 |
// Initialize custom license provider. |
| 43 |
require_once WINP_PLUGIN_DIR . '/includes/class.license.php'; |
| 44 |
$this->premium = new WINP_License(); |
| 45 |
|
| 46 |
require_once WINP_PLUGIN_DIR . '/includes/class.rest.php'; |
| 47 |
new WINP_Rest(); |
| 48 |
|
| 49 |
require_once WINP_PLUGIN_DIR . '/admin/pages/class.settings.php'; |
| 50 |
require_once WINP_PLUGIN_DIR . '/admin/pages/class.new-item.php'; |
| 51 |
require_once WINP_PLUGIN_DIR . '/admin/pages/class.snippet-library.php'; |
| 52 |
|
| 53 |
WINP_Settings::get_instance(); |
| 54 |
WINP_NewItem::get_instance(); |
| 55 |
WINP_SnippetLibrary::get_instance(); |
| 56 |
|
| 57 |
$this->load_global(); |
| 58 |
|
| 59 |
if ( is_admin() ) { |
| 60 |
|
| 61 |
if ( WINP_Helper::doing_ajax() ) { |
| 62 |
require WINP_PLUGIN_DIR . '/admin/ajax/ajax.php'; |
| 63 |
require WINP_PLUGIN_DIR . '/admin/ajax/snippet-library.php'; |
| 64 |
} |
| 65 |
|
| 66 |
$this->load_backend(); |
| 67 |
} |
| 68 |
add_action( |
| 69 |
'init', |
| 70 |
function () { |
| 71 |
if ( WINP_Plugin::app()->premium->is_active() ) { |
| 72 |
update_option( WINP_PLUGIN_NAMESPACE . '_logger_flag', 'yes' ); |
| 73 |
} |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
add_filter( WINP_PLUGIN_NAMESPACE . '_logger_data', [ $this, 'get_logger_data' ] ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get plugin instance |
| 82 |
* |
| 83 |
* @return WINP_Plugin |
| 84 |
*/ |
| 85 |
public static function app() { |
| 86 |
return self::$app; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Survey data. |
| 91 |
* |
| 92 |
* @return array<string, mixed> |
| 93 |
*/ |
| 94 |
public function get_survey_data() { |
| 95 |
$install_time = get_option( WINP_PLUGIN_NAMESPACE . '_install', time() ); |
| 96 |
$days_since_install = round( ( time() - $install_time ) / DAY_IN_SECONDS ); |
| 97 |
$total_snippets = wp_count_posts( WINP_SNIPPETS_POST_TYPE ); |
| 98 |
$total_snippets = isset( $total_snippets->publish ) ? $total_snippets->publish : 0; |
| 99 |
|
| 100 |
$data = [ |
| 101 |
'environmentId' => 'cmiooosih4vm0ad01eihjub64', |
| 102 |
'attributes' => [ |
| 103 |
'free_version' => WINP_PLUGIN_VERSION, |
| 104 |
'pro_version' => defined( 'WASP_PLUGIN_VERSION' ) ? WASP_PLUGIN_VERSION : '', |
| 105 |
'install_days_number' => $days_since_install, |
| 106 |
'license_status' => self::app()->premium->is_active(), |
| 107 |
'total_snippets' => $total_snippets, |
| 108 |
], |
| 109 |
]; |
| 110 |
|
| 111 |
if ( self::app()->premium->is_active() ) { |
| 112 |
$data['attributes']['license_key'] = apply_filters( 'themeisle_sdk_secret_masking', self::app()->premium->get_key() ); |
| 113 |
} |
| 114 |
|
| 115 |
return $data; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @return bool |
| 120 |
*/ |
| 121 |
public function current_user_car() { |
| 122 |
return current_user_can( 'manage_options' ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get Execute_Snippet object |
| 127 |
* |
| 128 |
* @return WINP_Execute_Snippet |
| 129 |
*/ |
| 130 |
public function get_execute_object() { |
| 131 |
require_once WINP_PLUGIN_DIR . '/includes/class.execute.snippet.php'; |
| 132 |
|
| 133 |
return WINP_Execute_Snippet::app(); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get WINP_Api object |
| 138 |
* |
| 139 |
* @return WINP_Api |
| 140 |
*/ |
| 141 |
public function get_api_object() { |
| 142 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.request.php'; |
| 143 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.api.php'; |
| 144 |
|
| 145 |
return new WINP_Api(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get WINP_Common_Snippet object |
| 150 |
* |
| 151 |
* @return WINP_Common_Snippet |
| 152 |
*/ |
| 153 |
public function get_common_object() { |
| 154 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.common.snippet.php'; |
| 155 |
|
| 156 |
return new WINP_Common_Snippet(); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Plugin activation hook. |
| 161 |
* Creates demo snippets on first activation and sets up capabilities. |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function activation_hook() { |
| 166 |
// Add custom capabilities to administrator role. |
| 167 |
$this->snippets_type->add_capabilities(); |
| 168 |
|
| 169 |
// Create demo snippets with examples of use. |
| 170 |
if ( ! get_option( 'wbcr_inp_demo_snippets_created' ) ) { |
| 171 |
WINP_Helper::create_demo_snippets(); |
| 172 |
} |
| 173 |
|
| 174 |
WINP_Helper::flush_page_cache(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Plugin deactivation hook. |
| 179 |
* Removes custom capabilities. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public function deactivation_hook() { |
| 184 |
// Remove custom capabilities from administrator role. |
| 185 |
$this->snippets_type->remove_capabilities(); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Register custom post types and taxonomies. |
| 190 |
* |
| 191 |
* @throws \Exception Exception. |
| 192 |
* @since 2.2.0 |
| 193 |
*/ |
| 194 |
private function register_types() { |
| 195 |
require_once WINP_PLUGIN_DIR . '/admin/types/snippets-post-types.php'; |
| 196 |
$this->snippets_type = new WINP_SnippetsType(); |
| 197 |
|
| 198 |
require_once WINP_PLUGIN_DIR . '/admin/types/snippets-taxonomy.php'; |
| 199 |
new WINP_SnippetsTaxonomy(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Register shortcodes. |
| 204 |
* |
| 205 |
* @since 2.2.0 |
| 206 |
*/ |
| 207 |
private function register_shortcodes() { |
| 208 |
$action = WINP_HTTP::get( 'action', '' ); |
| 209 |
if ( ! ( 'edit' == $action && is_admin() ) ) { |
| 210 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcodes.php'; |
| 211 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-php.php'; |
| 212 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-text.php'; |
| 213 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-universal.php'; |
| 214 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-css.php'; |
| 215 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-js.php'; |
| 216 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-html.php'; |
| 217 |
require_once WINP_PLUGIN_DIR . '/includes/shortcodes/shortcode-ad.php'; |
| 218 |
|
| 219 |
WINP_Helper::register_shortcode( 'WINP_SnippetShortcodePhp', $this ); |
| 220 |
WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeText', $this ); |
| 221 |
WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeUniversal', $this ); |
| 222 |
WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeCss', $this ); |
| 223 |
WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeJs', $this ); |
| 224 |
WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeHtml', $this ); |
| 225 |
WINP_Helper::register_shortcode( 'WINP_SnippetShortcodeAdvert', $this ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Initialization and require files for backend and frontend. |
| 231 |
* |
| 232 |
* @since 2.2.0 |
| 233 |
*/ |
| 234 |
private function load_global() { |
| 235 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.gutenberg.snippet.php'; |
| 236 |
require_once WINP_PLUGIN_DIR . '/includes/class.admin-bar.php'; |
| 237 |
|
| 238 |
new WINP_Gutenberg_Snippet(); |
| 239 |
WINP_Admin_Bar::instance(); |
| 240 |
|
| 241 |
$this->get_execute_object()->register_hooks(); |
| 242 |
$this->register_shortcodes(); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Initialization and require files for backend. |
| 247 |
* |
| 248 |
* @throws \Exception |
| 249 |
* @since 2.2.0 |
| 250 |
*/ |
| 251 |
private function load_backend() { |
| 252 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.snippets.viewtable.php'; |
| 253 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.filter.snippet.php'; |
| 254 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.actions.snippet.php'; |
| 255 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.notices.php'; |
| 256 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.admin.notices.php'; |
| 257 |
require_once WINP_PLUGIN_DIR . '/admin/includes/class.request.php'; |
| 258 |
require_once WINP_PLUGIN_DIR . '/admin/boot.php'; |
| 259 |
|
| 260 |
$this->get_common_object()->register_hooks(); |
| 261 |
$this->register_types(); |
| 262 |
|
| 263 |
new WINP_Filter_List(); |
| 264 |
new WINP_Actions_Snippet(); |
| 265 |
WINP_Notices::instance(); |
| 266 |
new WINP_Admin_Notices(); |
| 267 |
|
| 268 |
if ( ! defined( 'E2E_TESTING' ) ) { |
| 269 |
add_filter( |
| 270 |
'themeisle-sdk/survey/' . WINP_PLUGIN_SLUG, |
| 271 |
function ( $data, $page_slug ) { |
| 272 |
if ( empty( $page_slug ) ) { |
| 273 |
return $data; |
| 274 |
} |
| 275 |
|
| 276 |
return $this->get_survey_data(); |
| 277 |
}, |
| 278 |
10, |
| 279 |
2 |
| 280 |
); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Метод проверяет активацию премиум плагина и наличие действующего лицензионного ключа |
| 286 |
* |
| 287 |
* @return bool |
| 288 |
*/ |
| 289 |
public function is_premium() { |
| 290 |
return $this->premium->is_active(); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Logger data. |
| 295 |
* |
| 296 |
* @return array<string, mixed> |
| 297 |
*/ |
| 298 |
public function get_logger_data() { |
| 299 |
$settings = WINP_Settings::get_instance()->get_settings(); |
| 300 |
|
| 301 |
// Each settings has a lot of keys, we only want name and value items. So map them. |
| 302 |
$settings = array_map( |
| 303 |
function ( $setting ) { |
| 304 |
return [ |
| 305 |
'name' => isset( $setting['name'] ) ? $setting['name'] : '', |
| 306 |
'value' => isset( $setting['value'] ) ? $setting['value'] : '', |
| 307 |
]; |
| 308 |
}, |
| 309 |
$settings |
| 310 |
); |
| 311 |
|
| 312 |
$total_snippets = wp_count_posts( WINP_SNIPPETS_POST_TYPE ); |
| 313 |
$total_snippets = isset( $total_snippets->publish ) ? $total_snippets->publish : 0; |
| 314 |
|
| 315 |
// Count snippets by type. |
| 316 |
$snippet_types = [ 'php', 'css', 'js', 'text', 'html', 'advert', 'universal' ]; |
| 317 |
$types_count = []; |
| 318 |
|
| 319 |
foreach ( $snippet_types as $type ) { |
| 320 |
$count = get_posts( |
| 321 |
[ |
| 322 |
'post_type' => WINP_SNIPPETS_POST_TYPE, |
| 323 |
'post_status' => 'publish', |
| 324 |
'meta_key' => 'wbcr_inp_snippet_type', |
| 325 |
'meta_value' => $type, |
| 326 |
'posts_per_page' => -1, |
| 327 |
'fields' => 'ids', |
| 328 |
] |
| 329 |
); |
| 330 |
|
| 331 |
if ( ! empty( $count ) ) { |
| 332 |
$types_count[ $type ] = count( $count ); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return [ |
| 337 |
'settings' => $settings, |
| 338 |
'stats' => [ |
| 339 |
'total_snippets' => $total_snippets, |
| 340 |
'types' => $types_count, |
| 341 |
], |
| 342 |
]; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|