PluginProbe
Post Snippets – Custom WordPress Code Snippets Customizer / trunk
Post Snippets – Custom WordPress Code Snippets Customizer vtrunk
4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.1 1.7.2 1.7.3 1.8 1.8.1 1.8.2 1.8.3 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.8.9.1 1.8.9.2 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 All 115 releases
post-snippets / post-snippets.php

post-snippets.php in Post Snippets – Custom WordPress Code Snippets Customizer trunk, at post-snippets.php

467 lines 16.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Snippets
4 *
5 * @package PS
6 * @author Postsnippets <support@wpexperts.io>
7 * @license GPL-2.0+
8 * @link https://www.postsnippets.com
9 *
10 * @wordpress-plugin
11 * Plugin Name: Post Snippets (free)
12 * Plugin URI: https://www.postsnippets.com
13 * Description: Create a library of reusable content and insert it into your posts and pages. Navigate to "Settings > Post Snippets" to get started.
14 * Version: 4.2.4
15 * Author: Postsnippets
16 * Author URI: https://www.postsnippets.com
17 * License: GPL-2.0+
18 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
19 * Tested up to: 7.1
20 * Text Domain: post-snippets
21 * Domain Path: /lang
22 *
23 * @fs_premium_only /assets-pro/
24 */
25
26 if ( ! defined( 'ABSPATH' ) ) exit;
27
28 if ( !function_exists( 'postsnippets_fs' ) ) {
29 // Create a helper function for easy SDK access.
30 function postsnippets_fs()
31 {
32 global $postsnippets_fs ;
33
34 if ( !isset( $postsnippets_fs ) ) {
35 // Include Freemius SDK.
36 require_once dirname( __FILE__ ) . '/freemius/start.php';
37 $postsnippets_fs = fs_dynamic_init( array(
38 'id' => '1576',
39 'slug' => 'post-snippets',
40 'type' => 'plugin',
41 'public_key' => 'pk_58a2ec84c44485a459aae07bfaf5f',
42 'is_premium' => false,
43 // If your plugin is a serviceware, set this option to false.
44 'has_premium_version' => true,
45 'has_addons' => false,
46 'has_paid_plans' => true,
47 'trial' => array(
48 'days' => 14,
49 'is_require_payment' => true,
50 ),
51 'menu' => array(
52 'slug' => 'post-snippets',
53 'contact' => false,
54 'support' => false,
55 ),
56 'is_live' => true,
57 ) );
58 }
59
60 return $postsnippets_fs;
61 }
62
63
64
65
66
67
68 // Init Freemius.
69 postsnippets_fs();
70 // Signal that SDK was initiated.
71 do_action( 'postsnippets_fs_loaded' );
72 function postsnippets_fs_settings_url()
73 {
74 return admin_url( 'admin.php?page=post-snippets' );
75 }
76
77
78 postsnippets_fs()->add_action( 'after_uninstall', 'postsnippets_fs_uninstall_cleanup' );
79
80
81 function postsnippets_fs_uninstall_cleanup() {
82 $settings = get_option("post_snippets");
83 $complete_uninstall = isset($settings['complete_uninstall']) ? $settings['complete_uninstall'] : false;
84
85 if ($complete_uninstall) {
86
87 global $wpdb;
88
89 $wild = '%';
90 $find = "post_snippets";
91 $option_search = $wild . $wpdb->esc_like( $find ) . $wild;
92
93 $option_names = $wpdb->get_results($wpdb->prepare("SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE %s", $option_search), ARRAY_A );
94
95 if( !empty($option_names) ){
96 $option_names = array_column($option_names, 'option_name');
97 foreach ($option_names as $option_name) {
98
99 delete_option($option_name);
100
101 // for site options in Multisite
102 delete_site_option($option_name);
103
104 }
105 }
106 $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}pspro_snippets");
107 $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}pspro_groups");
108 }
109 }
110
111 // postsnippets_fs()->add_filter( 'connect_url', 'postsnippets_fs_settings_url' );
112 // postsnippets_fs()->add_filter( 'after_skip_url', 'postsnippets_fs_settings_url' );
113 // postsnippets_fs()->add_filter( 'after_connect_url', 'postsnippets_fs_settings_url' );
114 // postsnippets_fs()->add_filter( 'after_pending_connect_url', 'postsnippets_fs_settings_url' );
115 /**
116 * Only show Post Snippets submenu when PS is open, not on all Settings pages
117 */
118 function postsnippets_show_submenu( $is_visible )
119 {
120 if ( isset( $_REQUEST['page'] ) ) {
121 if ( $is_visible && false !== strpos( $_REQUEST['page'], 'post-snippets' ) ) {
122 return true;
123 }
124 }
125 return false;
126 }
127
128 postsnippets_fs()->add_filter(
129 'is_submenu_visible',
130 'postsnippets_show_submenu',
131 10,
132 2
133 );
134 if ( !defined( 'PS_MAIN_FILE' ) ) {
135 define( 'PS_MAIN_FILE', basename( __FILE__ ) );
136 }
137 if ( !defined( 'PS_VERSION' ) ) {
138 define( 'PS_VERSION', '4.2.4' );
139 }
140 if ( !defined( 'PS_MAIN_FILE_PATH' ) ) {
141 define( 'PS_MAIN_FILE_PATH', __FILE__ );
142 }
143 if ( !defined( 'PS_DIRECTORY' ) ) {
144 define( 'PS_DIRECTORY', plugin_basename( dirname( __FILE__ ) ) );
145 }
146 if ( !defined( 'PS_PATH' ) ) {
147 define( 'PS_PATH', plugin_dir_path( __FILE__ ) );
148 }
149 if ( !defined( 'PS_URL' ) ) {
150 define( 'PS_URL', plugins_url( '', __FILE__ ) . '/' );
151 }
152 if ( !defined( 'PS_MAIN_PAGE_URL' ) ) {
153 define( 'PS_MAIN_PAGE_URL', esc_url( admin_url( 'admin.php?page=post-snippets' ) ) );
154 }
155 class PostSnippets
156 {
157 /** Holds the plugin instance */
158 private static $instance = false ;
159 /** Define plugin constants */
160 const MIN_PHP_VERSION = '7.1' ;
161 const MIN_WP_VERSION = '5.3' ;
162 const SETTINGS = 'post_snippets' ;
163 const VERSION_KEY = 'post_snippets_version' ;
164 const OPTION_KEY = 'post_snippets_options' ;
165 const PS_ENABLE_REST = 'post_snippets_enable_rest' ;
166 const USER_META_KEY = 'post_snippets' ;
167 const FILE = __FILE__ ;
168
169 const TABLE_NAME = 'pspro_snippets';
170 const GROUP_TABLE_NAME = 'pspro_groups';
171 const GROUP_CLASS = 'PostSnippets\Group';
172 const EDIT_CLASS = 'PostSnippets\Edit';
173
174 const DUPLICATE_SETTINGS = 'duplicate_option' ;
175
176 /**
177 * Singleton class
178 */
179 public static function getInstance()
180 {
181 if ( !self::$instance ) {
182 self::$instance = new self();
183 }
184 return self::$instance;
185 }
186
187 /**
188 * Initializes the plugin.
189 */
190 private function __construct()
191 {
192 if ( !$this->testHost() ) {
193 return;
194 }
195 load_plugin_textdomain( 'post-snippets', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
196 add_action( 'after_setup_theme', array( &$this, 'phpExecState' ) );
197 add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_ai_chatbot_script' ) );
198 // add_action( 'admin_enqueue_scripts', array($this, 'enqueue_hide_seek_search_library') );
199
200 //requiring PS functions file
201 require_once 'src/PS_functions.php';
202 require_once 'views/integration/class_ps_ai.php';
203
204 new \PostSnippets\Admin();
205 new \PostSnippets\WPEditor();
206 new \PostSnippets\Shortcode();
207
208 if (postsnippets_fs()->can_use_premium_code() && file_exists(plugin_dir_path( self::FILE ).'/src/PostSnippets/PSRestAdmin.php')) {
209
210 new \PostSnippets\PSRestAdmin();
211
212 if ( 'yes' == get_option( self::PS_ENABLE_REST) && file_exists(plugin_dir_path( self::FILE ).'/src/PostSnippets/PSRestFunctions.php')) {
213
214 new \PostSnippets\PSRestFunctions();
215
216 }
217
218 }
219
220 }
221
222 /**
223 * PSR-0 compliant autoloader to load classes as needed.
224 *
225 * @param string $classname
226 *
227 * @return void
228 */
229 public static function autoload( $className )
230 {
231 if ( __CLASS__ !== mb_substr( $className, 0, strlen( __CLASS__ ) ) ) {
232 return;
233 }
234
235 $className = ltrim( $className, '\\' );
236 $fileName = '';
237 $namespace = '';
238
239 if ( $lastNsPos = strrpos( $className, '\\' ) ) {
240 $namespace = substr( $className, 0, $lastNsPos );
241 $className = substr( $className, $lastNsPos + 1 );
242 $fileName = str_replace( '\\', DIRECTORY_SEPARATOR, $namespace );
243 $fileName .= DIRECTORY_SEPARATOR;
244 }
245
246 $fileName .= str_replace( '_', DIRECTORY_SEPARATOR, $className );
247
248 require 'src' . DIRECTORY_SEPARATOR . $fileName . '.php';
249 }
250
251 // -------------------------------------------------------------------------
252 // Helpers
253 // -------------------------------------------------------------------------
254 /**
255 * Allow snippets to be retrieved directly from PHP.
256 *
257 * @since Post Snippets 1.8.9.1
258 *
259 * @param string $name The name of the snippet to retrieve
260 * @param string|array $variables The variables to pass to the snippet,
261 * formatted as a query string or an associative array.
262 *
263 * @return string The Snippet
264 */
265 public static function getSnippet( $name = '', $variables = '' )
266 {
267
268 if( empty($name) ){
269 return;
270 }
271
272 global $wpdb;
273 $table_name = $wpdb->prefix . \PostSnippets::TABLE_NAME;
274
275 $wild = '%';
276 $find = $name;
277 $snippet_title_like = $wild . $wpdb->esc_like( $find ) . $wild;
278
279 $particular_snippets = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE snippet_title LIKE %s", $snippet_title_like), ARRAY_A );
280
281 if( empty($particular_snippets) ){
282 return;
283 }
284
285 foreach($particular_snippets as $particular_snippet){ /**Multiple Snippets of Similar Name (Case Sensitive) */
286
287 if( strcmp($particular_snippet['snippet_title'], $name) == 0 && !empty($particular_snippet) ){
288 $snippet = $particular_snippet;
289 break;
290 }
291 }
292
293 if( empty( $snippet ) || empty( $snippet['snippet_content'] ) ){
294 return;
295 }
296
297 $snippet_content = $snippet['snippet_content'];
298 if ( !is_array( $variables ) ) {
299 parse_str( htmlspecialchars_decode( $variables ), $variables );
300 }
301
302 $default_atts = \PostSnippets\Shortcode::filterVars( $snippet['snippet_vars'] );
303 $short_atts = shortcode_atts( $default_atts, $variables );
304
305 if( !empty($short_atts) ){
306 foreach ($short_atts as $key => $val) {
307 if( !empty($val) ){
308 $snippet_content = str_replace( "{" . $key . "}", $val, $snippet_content );
309 }
310 }
311 }
312
313 return do_shortcode( $snippet_content );
314 }
315
316 // -------------------------------------------------------------------------
317 // Environment Checks
318 // -------------------------------------------------------------------------
319 /**
320 * Checks PHP and WordPress versions.
321 */
322 private function testHost()
323 {
324 // Check if PHP is too old
325
326 if ( version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '<' ) ) {
327 // Display notice
328 add_action( 'admin_notices', array( &$this, 'phpVersionError' ) );
329 return false;
330 }
331
332 // Check if WordPress is too old
333 global $wp_version ;
334
335 if ( version_compare( $wp_version, self::MIN_WP_VERSION, '<' ) ) {
336 add_action( 'admin_notices', array( &$this, 'wpVersionError' ) );
337 return false;
338 }
339
340 return true;
341 }
342
343 /**
344 * Displays a warning when installed on an old PHP version.
345 */
346 public function phpVersionError()
347 {
348 echo '<div class="error"><p><strong>' ;
349 printf(
350 'Error: %3$s requires PHP version %1$s or greater.<br/>' . 'Your installed PHP version: %2$s',
351 self::MIN_PHP_VERSION,
352 PHP_VERSION,
353 $this->getPluginName()
354 );
355 echo '</strong></p></div>' ;
356 }
357
358 /**
359 * Displays a warning when installed in an old WordPress version.
360 */
361 public function wpVersionError()
362 {
363 echo '<div class="error"><p><strong>' ;
364 printf( 'Error: %2$s requires WordPress version %1$s or greater.', self::MIN_WP_VERSION, $this->getPluginName() );
365 echo '</strong></p></div>' ;
366 }
367
368 /**
369 * Get the name of this plugin.
370 *
371 * @return string The plugin name.
372 */
373 private function getPluginName()
374 {
375 $data = get_plugin_data( self::FILE );
376 return $data['Name'];
377 }
378
379 // -------------------------------------------------------------------------
380 // Deprecated methods
381 // -------------------------------------------------------------------------
382 /**
383 * Allow plugins to disable the PHP Code execution feature with a filter.
384 * Deprecated: Use the POST_SNIPPETS_DISABLE_PHP global constant to disable
385 * PHP instead.
386 *
387 * @see http://wordpress.org/extend/plugins/post-snippets/faq/
388 * @since 2.1
389 * @deprecated 2.3
390 */
391 public function phpExecState()
392 {
393 $filter = apply_filters( 'post_snippets_php_execution_enabled', true );
394
395 if ( $filter == false and !defined( 'POST_SNIPPETS_DISABLE_PHP' ) ) {
396 _deprecated_function( 'post_snippets_php_execution_enabled', '2.3', 'define(\'POST_SNIPPETS_DISABLE_PHP\', true);' );
397 define( 'POST_SNIPPETS_DISABLE_PHP', true );
398 }
399
400 }
401
402 /*
403 public function enqueue_hide_seek_search_library() {
404 wp_enqueue_script('jquery-hideseek', plugin_dir_url( __FILE__ ) . 'assets-pro/jquery.hideseek.js', array( 'jquery' ), '1.0.0', true );
405 }
406 */
407
408
409
410 /**
411 * routines to run after the plugin is activated.
412 */
413 public static function post_snippet_pro_activated() {
414
415 /**Create DB Table, if not already */
416 new \PostSnippets\DBTable();
417
418
419 }
420
421 /**
422 * routines to run to check whether the plugin is activated .
423 */
424 public static function post_snippet_pro_update_check() {
425
426 $current_plugin_verion = get_option( self::VERSION_KEY );
427 $current_db_version = get_option( 'psp_db_version' );
428 $target_db_version = ( new \PostSnippets\DBTable() )->psp_db_version;
429
430 if ( ! $current_plugin_verion || version_compare( $current_plugin_verion, PS_VERSION, '<' ) || version_compare( (string) $current_db_version, $target_db_version, '<' ) ) {
431
432 /**Create or update DB Table */
433 new \PostSnippets\DBTable();
434 update_option( self::VERSION_KEY, PS_VERSION );
435 }
436
437 }
438
439 public function enqueue_ai_chatbot_script() {
440 wp_register_script( 'chatbot-script', plugins_url( '/assets/chatbot-script.js', \PostSnippets::FILE ), array( 'jquery' ), PS_VERSION, true );
441 wp_localize_script(
442 'chatbot-script',
443 'PSchatbot',
444 array(
445 'ajaxurl' => admin_url( 'admin-ajax.php' ),
446 'nonce' => wp_create_nonce('chatbot_nonce'),
447 'settings_url' => admin_url( 'admin.php?page=post-snippets-chatbot' )
448 )
449 );
450
451 wp_enqueue_script( 'chatbot-script' );
452 }
453
454 }
455 add_action( 'plugins_loaded', array( 'PostSnippets', 'getInstance' ) );
456 add_action( 'init', array( 'PostSnippets', 'post_snippet_pro_update_check' ), 20 ); /**Check For Version and Update Accordingly */
457
458 /**
459 * Load all of the necessary class files for the plugin
460 */
461 spl_autoload_register( 'PostSnippets::autoload' );
462
463 register_activation_hook( __FILE__, array( 'PostSnippets', 'post_snippet_pro_activated' ) );
464
465
466
467 }