save_data(); $this->perform_actions_response(); $this->setup_code_editor(); $this->edit_snippets_meta_boxes(); $this->setup_sections(); $this->editPage(); } public static function prepare(){ if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit' && isset($_REQUEST['snippet']) ){ self::$snippet_id = intval($_REQUEST['snippet']); check_admin_referer( 'ps-snippets-hover-trigger_'.self::$snippet_id ); global $wpdb; if( !$wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix.\PostSnippets::TABLE_NAME." WHERE ID = %d", self::$snippet_id) ) ){ self::$snippet_id = ''; wp_safe_redirect( admin_url('admin.php?page=post-snippets' ) ); } if( !empty(self::$snippet_id) ){ self::$editSnippetPage = true; } } } public static function change_edit_snippet_page_title($admin_title, $title){ self::prepare(); if(self::$editSnippetPage){ $admin_title = str_replace( __("Add New", "post-snippets"), __("Edit Snippet", "post-snippets"), $admin_title); return $admin_title; } return $admin_title; } function editPage(){ ?>

Snippet

" class="page-title-action">

Snippet


body_content_cb(); ?>

%s..

', esc_html__( 'Snippet Added Successfully', 'post-snippets' ) ); } } /** * Prints the jQuery script to initiliase the metaboxes * Called on admin_footer-* */ public static function footer_scripts(){ ?> 2, 'default' => 2) ); /* Enqueue WordPress' script for handling the metaboxes */ wp_enqueue_script('postbox'); } public static function edit_snippets_meta_boxes(){ add_meta_box( 'ps_action_meta_box', //Meta box ID __('Actions','post-snippets'), //Meta box Title array(\PostSnippets::EDIT_CLASS, 'actions_metabox_content'), //Callback defining the metabox's innards NULL, // Screen to which to add the meta box, blank means current screen 'side' // Context ); if( isset($_REQUEST['page']) && $_REQUEST['page'] === 'post-snippets-edit-js' || $_REQUEST['page'] === 'post-snippets-edit-css'){ add_meta_box( 'ps_option_meta_box', //Meta box ID __('Options','post-snippets'), //Meta box Title array(\PostSnippets::EDIT_CLASS, 'options_metabox_content'), //Callback defining the metabox's innards NULL, // Screen to which to add the meta box, blank means current screen 'side' // Context ); add_meta_box( 'ps_preview_meta_box', //Meta box ID __('Preview','post-snippets'), //Meta box Title array(\PostSnippets::EDIT_CLASS, 'preview_metabox_content'), //Callback defining the metabox's innards NULL, // Screen to which to add the meta box, blank means current screen 'normal' // Context ); add_meta_box( 'ps_pages_meta_box', //Meta box ID __('Apply only on these URLs','post-snippets'), //Meta box Title array(\PostSnippets::EDIT_CLASS, 'pages_metabox_content'), //Callback defining the metabox's innards NULL, // Screen to which to add the meta box, blank means current screen 'normal' // Context ); } } function body_content_cb(){ $this->snippetTitle(); $this->codeEditor(); // $this->setup_sections(); if( isset($_REQUEST['page']) && $_REQUEST['page'] === 'post-snippets-edit'){ do_settings_sections( 'post-snippets-edit' ); } $this->description(); // submit_button(__('Save','post-snippets')); } static function actions_metabox_content(){ if( !self::$editSnippetPage ){ $disabled_action = "style = opacity:40%"; $disabled_action_cursor = "style = cursor:not-allowed"; } else{ $disabled_action = ''; $disabled_action_cursor = ''; } ?>
>
prefix.\PostSnippets::GROUP_TABLE_NAME; $all_groups = $wpdb->get_results($wpdb->prepare("SELECT * FROM %1s", $table_name), ARRAY_A ); ?>

>
>

$available_site_location) { ?> >
$location_page); } if( !in_array( $location_type, $available_locations_slugs, true ) ){ return ''; } return $location_type; } else{ $location = ''; } } else{ $location = ''; } return $location; } return ''; } static function getAllPagesAndPosts(){ $args = array( 'post_type' => array( 'page', 'post' ), 'numberposts' => -1 ); $pages = get_posts($args); return $pages; } static function isPageOrPost( $id = '' ){ if( empty($id) ){ return false; } $pagesAndPosts = self::getAllPagesAndPosts(); if( !empty( $pagesAndPosts ) ){ foreach ($pagesAndPosts as $pageAndPost) { if($id == $pageAndPost->ID){ return true; } continue; } return false; } return false; } function snippetTitle(){ ?>
'application/x-httpd-php', // 'type' => 'text/javascript', 'type' => $type, 'codemirror' => array( // 'value' => ''; // $snippet_content = $start.'\n'.$snippet_content.'\n'.$end; // } //****************************** END *********************************** */ //******************** Preparing Snippet Description ************************* */ $allowed_html = apply_filters( 'ps_snippet_desc_allowed_html', array( 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(), 'b' => array(), 'strong' => array(), 'pre' => array(), 'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(), )); $snippet_desc = wp_kses( $_REQUEST['snippet_desc'] ?? '', $allowed_html); //****************************** END *********************************** */ //******************** Preparing Snippet PHP ************************* */ if( isset($_REQUEST['snippet_php']) ){ $snippet_php = ( ($_REQUEST['snippet_php']?? 0 ) == 1) ? 1 : 0; } else{ /**As this variable is not used in JS and CSS pages, they * can be used to save those pages. so new DB entry * doesn't need to be created. * 2 for JS and 3 for CSS. */ if( $this->is_JS() ){ $snippet_php = 2; } elseif( $this->is_CSS() ){ $snippet_php = 3; } else{ // return false; $snippet_php = '0'; } } //****************************** END *********************************** */ if( !empty($snippet_title) ){ if(self::$editSnippetPage){ /**This is a edit page of Previous Snippet */ $current_snippet_title = $wpdb->get_var( $wpdb->prepare("SELECT snippet_title FROM $table_name WHERE ID = %d", self::$snippet_id) ); if( $snippet_title != $current_snippet_title && $this->duplicate_title_check($snippet_title) ){ printf('

%s..

', esc_html__( 'Duplicate Titles are Not Allowed', 'post-snippets' ) ); return; } else{ $current_group_list = $group_list; $previous_group_list = maybe_unserialize( $wpdb->get_var( $wpdb->prepare("SELECT snippet_group FROM $table_name WHERE ID = %d", self::$snippet_id) ) ); $increment_count = array_diff($current_group_list, $previous_group_list); $decrement_count = array_diff($previous_group_list, $current_group_list); $snippet_updated = $wpdb->update( $table_name, array( 'snippet_group' => maybe_serialize( $group_list ), 'snippet_title' => $snippet_title, 'snippet_content' => $snippet_content, 'snippet_date' => current_time( 'mysql' ), 'snippet_vars' => $snippet_vars, 'snippet_desc' => $snippet_desc, 'snippet_status' => ( ($_REQUEST['snippet_status']?? 0 ) == 1) ? 1 : 0, //Disable by default 'snippet_shortcode' => ( ($_REQUEST['snippet_shortcode']?? 0 ) == 1) ? 1 : 0, 'snippet_php' => $snippet_php, 'snippet_wptexturize' => ( ($_REQUEST['snippet_wptexturize']?? 0 ) == 1) ? 1 : 0, ), array( /**Where Coulum = ? */ 'ID' => self::$snippet_id, ), array( /**Data Format, %d or %s */ '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d' ), array( /**Where Format, %d or %s */ '%d' ) ); if($snippet_updated){ printf('

%s..

', esc_html__( 'Snippet Updated', 'post-snippets' ) ); // return; } elseif(!$snippet_updated){ printf('

%s..

', esc_html__( 'Snippet Update Failed', 'post-snippets' ) ); // return; } if( !empty($increment_count) || !empty($decrement_count) ){ //change group count here if( !empty($increment_count) ){ /**Increment Count of these groups */ self::change_group_count($increment_count, 'increment'); } if( !empty($decrement_count) ){ /**Decrement Count of these groups */ self::change_group_count($decrement_count, 'decrement'); } } } } elseif( !self::$editSnippetPage && !$this->duplicate_title_check($snippet_title) ){ /**Adding New Snippet here */ $snippet_added = $wpdb->insert( $table_name, array( 'snippet_group' => maybe_serialize( $group_list ), 'snippet_title' => $snippet_title, 'snippet_content' => $snippet_content, 'snippet_date' => current_time( 'mysql' ), 'snippet_vars' => $snippet_vars, 'snippet_desc' => $snippet_desc, 'snippet_status' => ( ($_REQUEST['snippet_status']?? 0 ) == 1) ? 1 : 0, //Disable by default 'snippet_shortcode' => ( ($_REQUEST['snippet_shortcode']?? 0 ) == 1) ? 1 : 0, 'snippet_php' => $snippet_php, 'snippet_wptexturize' => ( ($_REQUEST['snippet_wptexturize']?? 0 ) == 1) ? 1 : 0, ) ); if( $snippet_added ){ self::change_group_count($group_list, 'increment'); $page = self::get_snippet_page(); $url = admin_url( sprintf('admin.php?page=%s&action=edit&snippet=%s&message=%s&_wpnonce=%s', $page, $wpdb->insert_id, 1, wp_create_nonce('ps-snippets-hover-trigger_'.$wpdb->insert_id) ) ); ?>

%s..

', esc_html__( 'Snippet Not Added', 'post-snippets' ) ); } } else{ printf('

%s..

', esc_html__( 'Duplicate Titles are Not Allowed', 'post-snippets' ) ); } } else{ printf('

%s..

', esc_html__( 'Empty Title is Not Allowed', 'post-snippets' ) ); } } } public static function get_snippet_page($id=''){ if(empty($id)){ if( isset($_REQUEST['page']) && ( $_REQUEST['page'] === 'post-snippets-edit' || $_REQUEST['page'] === 'post-snippets-edit-js' || $_REQUEST['page'] === 'post-snippets-edit-css' ) ){ return $_REQUEST['page']; } return 'post-snippets-edit'; }else{ if($id == 2){ return 'post-snippets-edit-js'; } elseif($id == 3){ return 'post-snippets-edit-css'; } elseif($id == 0 || $id == 1){ return 'post-snippets-edit'; } return 'post-snippets-edit'; } } public static function duplicate_title_check($snippet_title){ global $wpdb; $table_name = $wpdb->prefix.\PostSnippets::TABLE_NAME; $all_snippets_titles = $wpdb->get_results( $wpdb->prepare("SELECT snippet_title FROM %1s", $table_name), ARRAY_A ); $all_snippets_titles = array_column($all_snippets_titles, 'snippet_title'); $duplicate_title_exist = in_array($snippet_title, $all_snippets_titles); if( $duplicate_title_exist ){ /**Cause This function is also used to Increment * Title Count by number_duplicate_snippet_title, * otherwise, above written code would have to be * repeated. **/ return $all_snippets_titles; } else{ return false; } } public static function number_duplicate_snippet_title($snippet_title){ if( empty($snippet_title) || !is_string($snippet_title) ){ //redundancy $snippet_title = __('Untitled', 'post-snippets'); } $all_snippets_titles = self::duplicate_title_check($snippet_title); if(!$all_snippets_titles){ return $snippet_title; } return self::increment_snippet_title( $all_snippets_titles, $snippet_title ); } public static function increment_snippet_title( $all_snippets_titles, $snippet_title ){ /**Searching For Similar Titles */ $same_titles = array(); foreach ($all_snippets_titles as $title) { if( strpos( $title, $snippet_title ) === 0 ){ $same_titles[] = $title; } } /**Incrementing Said Title */ $counter = 1; while($counter){ if( in_array($snippet_title.'_'.$counter, $same_titles) ){ $counter++; } else{ break; } } $new_snippet_title = $snippet_title.'_'.$counter; return $new_snippet_title; } static function generate_shortcode_text(){ if(self::$editSnippetPage){ $snippet_vars = Shortcode::filterVars( self::get_data('snippet_vars') ); $snippet_vars_text = ''; foreach ($snippet_vars as $var => $value) { $snippet_vars_text .= ' '. $var . (!empty($value)?'=':'=""') . $value; } $shortcode = '['.self::get_data('snippet_title') . $snippet_vars_text . ']'; return $shortcode; } } function perform_actions_response(){ if(self::$editSnippetPage){ if( isset($_REQUEST['download']) ){ check_admin_referer( 'pspro_edit_snippet', 'pspro_edit_snippet_nonce' ); $ie = new ImportExport(); $download_succeed = $ie->exportSnippets("(".self::$snippet_id.")"); if($download_succeed){ printf('

%s..

', esc_html__( 'Snippets Downloaded', 'post-snippets' ) ); } else{ printf('

%s..

', esc_html__( 'There has been an Error', 'post-snippets' ) ); } } if( isset($_REQUEST['upload']) ){ check_admin_referer( 'pspro_edit_snippet', 'pspro_edit_snippet_nonce' ); } if( isset($_REQUEST['duplicate']) ){ check_admin_referer( 'pspro_edit_snippet', 'pspro_edit_snippet_nonce' ); if( PSallSnippets::duplicate_snippet(self::$snippet_id) ){ printf('

%s..

', esc_html__( 'Snippet Duplicated', 'post-snippets' ) ); return; } else{ printf('

%s..

', esc_html__( 'There has been an Error', 'post-snippets' ) ); } } } } public static function addImportedSnippet( $imported_snippet = array() ){ if( !empty($imported_snippet) ){ global $wpdb; $table_name = $wpdb->prefix.\PostSnippets::TABLE_NAME; $snippet_php = PSallSnippets::get_snippet_php($imported_snippet['snippet_php']); // Sanitize imported snippet content to prevent XSS $sanitized_content = self::sanitize_snippet_content( $imported_snippet['snippet_content'] ?? '', $snippet_php ); $snippet_added = $wpdb->insert( $table_name, array( 'snippet_group' => $imported_snippet['snippet_group'], 'snippet_title' => self::filter_snippet_title( $imported_snippet['snippet_title'] ), 'snippet_content' => $sanitized_content, 'snippet_date' => current_time( 'mysql' ), 'snippet_vars' => self::filter_snippet_vars( $imported_snippet['snippet_vars'] ), 'snippet_desc' => sanitize_textarea_field($imported_snippet['snippet_desc'] ?? ''), 'snippet_status' => (($imported_snippet['snippet_status']?? 0) == 1) ? 1 : 0, //Disable by default 'snippet_shortcode' => (($imported_snippet['snippet_shortcode']?? 0) == 1) ? 1 : 0, 'snippet_php' => $snippet_php, 'snippet_wptexturize' => (($imported_snippet['snippet_wptexturize']?? 0) == 1) ? 1 : 0, ) ); if($snippet_added){ self::change_group_count( maybe_unserialize( $imported_snippet['snippet_group'] ), 'increment'); return true; } else{ return false; } } else{ return false; } } public static function filter_snippet_vars($vars = ''){ if( !empty($vars) ){ $string = $vars; $pattern = '/([^A-Za-z,=:_\d])/i'; /**remove any character from vars that are not Letters, Numbers or digits or Illegal php variable name */ $replacement = ''; $snippet_vars = preg_replace($pattern, $replacement, $string); return $snippet_vars; } else{ return $vars; } } public static function filter_snippet_title($title = ''){ $title = sanitize_text_field( $title ); if( !isset($title) || empty($title) ){ // $snippet_title = __( 'Untitled', 'post-snippets' ); $snippet_title = self::number_duplicate_snippet_title(__('Untitled', 'post-snippets')); } else{ $snippet_title = sanitize_text_field( $title ); } $pattern = '/[\s]/i'; /**remove any WhiteSpaces */ $replacement = ''; $snippet_title = preg_replace($pattern, $replacement, $snippet_title); return $snippet_title; } /** * Sanitize snippet content based on snippet type * * @param string $content The snippet content to sanitize * @param int $snippet_php The snippet type (0=regular, 1=PHP, 2=JS, 3=CSS) * @return string Sanitized content */ public static function sanitize_snippet_content($content, $snippet_php = 0) { if (empty($content)) { return $content; } // For JavaScript snippets, validate and sanitize dangerous patterns if ($snippet_php == 2) { // Remove potential XSS vectors while preserving legitimate JavaScript code $dangerous_patterns = array( '/]*>/i', // Remove script tags (they shouldn't be in content) '/<\/script>/i', // Remove closing script tags '/javascript\s*:/i', // Remove javascript: protocol '/on\w+\s*=/i', // Remove event handlers (onclick, onerror, etc.) '/