Changeset 3313497
- Timestamp:
- 06/17/2025 08:25:17 PM (10 months ago)
- Location:
- elevation-blocks
- Files:
-
- 2 edited
-
tags/1.0.4/lib/admin/controls/class-helpers.php (modified) (30 diffs)
-
trunk/lib/admin/controls/class-helpers.php (modified) (30 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elevation-blocks/tags/1.0.4/lib/admin/controls/class-helpers.php
r3313416 r3313497 1 1 <?php 2 2 3 /** 3 4 * File Name: class-helpers.php … … 10 11 namespace ElevationBlocks\Lib\Admin\Controls; 11 12 12 if ( ! defined( 'ABSPATH' )) {13 if (! defined('ABSPATH')) { 13 14 exit; 14 15 } … … 17 18 * Class Helpers 18 19 */ 19 class Helpers { 20 class Helpers 21 { 20 22 21 23 … … 32 34 * @return void 33 35 */ 34 public function __construct() { 35 add_action( 'admin_menu', array( $this, 'elevation_blocks_import_admin_menu' ) ); 36 public function __construct() 37 { 38 add_action('admin_menu', array($this, 'elevation_blocks_import_admin_menu')); 36 39 } 37 40 … … 41 44 * @return html 42 45 */ 43 public function elevation_blocks_import_page_html() { 44 if ( ! current_user_can( 'manage_options' ) ) { 46 public function elevation_blocks_import_page_html() 47 { 48 if (! current_user_can('manage_options')) { 45 49 return; 46 50 } 47 51 48 52 // Handle import action. 49 if ( isset( $_POST['import_demo_content'] )) {53 if (isset($_POST['import_demo_content'])) { 50 54 // Unslash the data before verification. 51 $import_demo_nonce = isset( $_POST['import_demo_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['import_demo_nonce'] )) : '';55 $import_demo_nonce = isset($_POST['import_demo_nonce']) ? sanitize_text_field(wp_unslash($_POST['import_demo_nonce'])) : ''; 52 56 53 57 // Verify the nonce after unslashing. 54 if ( ! wp_verify_nonce( $import_demo_nonce, 'import_demo_nonce_action' )) {55 wp_die( esc_html__( 'Nonce verification failed', 'elevation-blocks' ));58 if (! wp_verify_nonce($import_demo_nonce, 'import_demo_nonce_action')) { 59 wp_die(esc_html__('Nonce verification failed', 'elevation-blocks')); 56 60 } 57 61 … … 64 68 } 65 69 66 ?>70 ?> 67 71 <div class="wrap"> 68 <h1><?php echo esc_html__( 'Import Elevation Demo Content', 'elevation-blocks'); ?></h1>69 <p><?php echo esc_html__( 'Click the button below to import default pages for your theme.', 'elevation-blocks'); ?></p>72 <h1><?php echo esc_html__('Import Elevation Demo Content', 'elevation-blocks'); ?></h1> 73 <p><?php echo esc_html__('Click the button below to import default pages for your theme.', 'elevation-blocks'); ?></p> 70 74 <form method="post"> 71 <?php wp_nonce_field( 'import_demo_nonce_action', 'import_demo_nonce'); ?>72 <input type="submit" name="import_demo_content" class="button button-primary" value="<?php echo esc_attr__( 'Import Pages', 'elevation-blocks'); ?>" onclick="return confirm('Are you sure you want to import demo content? This could overwrite existing content.');">75 <?php wp_nonce_field('import_demo_nonce_action', 'import_demo_nonce'); ?> 76 <input type="submit" name="import_demo_content" class="button button-primary" value="<?php echo esc_attr__('Import Pages', 'elevation-blocks'); ?>" onclick="return confirm('Are you sure you want to import demo content? This could overwrite existing content.');"> 73 77 </form> 74 78 </div> 75 <?php79 <?php 76 80 } 77 81 … … 81 85 * @return void 82 86 */ 83 public function elevation_blocks_import_admin_menu() { 87 public function elevation_blocks_import_admin_menu() 88 { 84 89 add_management_page( 85 esc_html__( 'Import Elevation Demo Content', 'elevation-blocks'), // Page title.86 esc_html__( 'Import Elevation Demo Content', 'elevation-blocks'), // Menu title.90 esc_html__('Import Elevation Demo Content', 'elevation-blocks'), // Page title. 91 esc_html__('Import Elevation Demo Content', 'elevation-blocks'), // Menu title. 87 92 'manage_options', // Capability. 88 93 'elevation-import', // Menu slug. 89 array( $this, 'elevation_blocks_import_page_html'), // Callback function.94 array($this, 'elevation_blocks_import_page_html'), // Callback function. 90 95 ); 91 96 } … … 96 101 * @return void 97 102 */ 98 public function elevation_blocks_import_demo_pages() { 103 public function elevation_blocks_import_demo_pages() 104 { 99 105 // Define the paths to the images. 100 106 $image_paths = array( 101 'home-banner' => plugin_dir_url( __DIR__ ). 'assets/matthew-henry-2Ts5HnA67k8-unsplash.avif',102 'home-second-section' => plugin_dir_url( __DIR__ ). 'assets/matthew-henry-U5rMrSI7Pn4-unsplash.avif',103 'about-banner' => plugin_dir_url( __DIR__ ). 'assets/paulo-simoes-mendes-V8YzvXKLwDw-unsplash.avif',104 'contact-banner' => plugin_dir_url( __DIR__ ). 'assets/samantha-sophia-NaWKMlp3tVs-unsplash.avif',107 'home-banner' => ELEVATION_BLOCKS_DIR . 'assets/matthew-henry-2Ts5HnA67k8-unsplash.avif', 108 'home-second-section' => ELEVATION_BLOCKS_DIR . 'assets/matthew-henry-U5rMrSI7Pn4-unsplash.avif', 109 'about-banner' => ELEVATION_BLOCKS_DIR . 'assets/paulo-simoes-mendes-V8YzvXKLwDw-unsplash.avif', 110 'contact-banner' => ELEVATION_BLOCKS_DIR . 'assets/samantha-sophia-NaWKMlp3tVs-unsplash.avif', 105 111 ); 106 112 113 107 114 $uploaded_images = array(); 108 foreach ( $image_paths as $key => $image_path) {109 $uploaded_images[ $key ] = $this->elevation_blocks_upload_image( $image_path);115 foreach ($image_paths as $key => $image_path) { 116 $uploaded_images[$key] = $this->elevation_blocks_upload_image($image_path); 110 117 } 111 118 … … 113 120 array( 114 121 'title' => 'Home', 115 'content' => $this->elevation_blocks_home_page( $uploaded_images),122 'content' => $this->elevation_blocks_home_page($uploaded_images), 116 123 ), 117 124 array( 118 125 'title' => 'About', 119 'content' => $this->elevation_blocks_about_page( $uploaded_images),126 'content' => $this->elevation_blocks_about_page($uploaded_images), 120 127 ), 121 128 array( 122 129 'title' => 'Contact', 123 'content' => $this->elevation_blocks_contact_page( $uploaded_images),130 'content' => $this->elevation_blocks_contact_page($uploaded_images), 124 131 ), 125 132 ); 126 133 127 foreach ( $default_pages as $page) {134 foreach ($default_pages as $page) { 128 135 // Check if the page already exists. 129 136 $page_query = new \WP_Query( … … 138 145 $existing_page = $page_query->have_posts(); 139 146 140 if ( ! $existing_page) {147 if (! $existing_page) { 141 148 $page_id = wp_insert_post( 142 149 array( … … 148 155 ); 149 156 150 if ( 'Home' === $page['title']) {151 update_option( 'show_on_front', 'page'); // Set static page mode.152 update_option( 'page_on_front', $page_id); // Set the front page.157 if ('Home' === $page['title']) { 158 update_option('show_on_front', 'page'); // Set static page mode. 159 update_option('page_on_front', $page_id); // Set the front page. 153 160 } 154 161 } … … 163 170 * @return array 164 171 */ 165 private function elevation_blocks_upload_image( $file_path ) { 166 if ( ! file_exists( $file_path ) ) { 172 private function elevation_blocks_upload_image($file_path) 173 { 174 if (! file_exists($file_path)) { 167 175 return false; 168 176 } 169 177 170 $file_name = basename( $file_path);178 $file_name = basename($file_path); 171 179 $upload_dir = wp_upload_dir(); 172 180 $new_file_path = $upload_dir['path'] . '/' . $file_name; 173 181 174 if ( ! copy( $file_path, $new_file_path )) {182 if (! copy($file_path, $new_file_path)) { 175 183 return false; 176 184 } 177 185 178 $file_type = wp_check_filetype( $new_file_path, null);186 $file_type = wp_check_filetype($new_file_path, null); 179 187 $attachment = array( 180 188 'post_mime_type' => $file_type['type'], 181 'post_title' => sanitize_file_name( $file_name),189 'post_title' => sanitize_file_name($file_name), 182 190 'post_content' => '', 183 191 'post_status' => 'inherit', 184 192 ); 185 193 186 $attach_id = wp_insert_attachment( $attachment, $new_file_path);194 $attach_id = wp_insert_attachment($attachment, $new_file_path); 187 195 require_once ABSPATH . 'wp-admin/includes/image.php'; 188 $attach_data = wp_generate_attachment_metadata( $attach_id, $new_file_path);189 wp_update_attachment_metadata( $attach_id, $attach_data);190 191 return array( $attach_id, wp_get_attachment_url( $attach_id )); // Return the image URL.196 $attach_data = wp_generate_attachment_metadata($attach_id, $new_file_path); 197 wp_update_attachment_metadata($attach_id, $attach_data); 198 199 return array($attach_id, wp_get_attachment_url($attach_id)); // Return the image URL. 192 200 } 193 201 … … 199 207 * @return array 200 208 */ 201 private function elevation_blocks_upload_image_from_url( $image_url ) { 202 if ( ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) { 209 private function elevation_blocks_upload_image_from_url($image_url) 210 { 211 212 if (! filter_var($image_url, FILTER_VALIDATE_URL)) { 203 213 return false; 204 214 } 205 215 206 216 // Get file contents. 207 $response = wp_remote_get( $image_url ); 208 209 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { 217 $response = wp_remote_get($image_url); 218 219 220 221 if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { 210 222 return false; 211 223 } 212 224 213 $image_data = wp_remote_retrieve_body( $response);214 if ( empty( $image_data )) {225 $image_data = wp_remote_retrieve_body($response); 226 if (empty($image_data)) { 215 227 return false; 216 228 } 217 229 218 230 // Extract filename from URL. 219 $file_name = basename( wp_parse_url( $image_url, PHP_URL_PATH ));231 $file_name = basename(wp_parse_url($image_url, PHP_URL_PATH)); 220 232 221 233 // Get upload directory. 222 234 $upload_dir = wp_upload_dir(); 223 $new_file_path = trailingslashit( $upload_dir['path']) . $file_name;235 $new_file_path = trailingslashit($upload_dir['path']) . $file_name; 224 236 225 237 // Use WP_Filesystem to save the file. … … 229 241 WP_Filesystem(); 230 242 231 if ( ! $wp_filesystem->put_contents( $new_file_path, $image_data, FS_CHMOD_FILE )) {243 if (! $wp_filesystem->put_contents($new_file_path, $image_data, FS_CHMOD_FILE)) { 232 244 return false; 233 245 } 234 246 235 247 // Get file type. 236 $file_type = wp_check_filetype( $file_name, null);248 $file_type = wp_check_filetype($file_name, null); 237 249 238 250 // Create attachment. 239 251 $attachment = array( 240 252 'post_mime_type' => $file_type['type'], 241 'post_title' => sanitize_file_name( $file_name),253 'post_title' => sanitize_file_name($file_name), 242 254 'post_content' => '', 243 255 'post_status' => 'inherit', 244 256 ); 245 257 246 $attach_id = wp_insert_attachment( $attachment, $new_file_path ); 258 $attach_id = wp_insert_attachment($attachment, $new_file_path); 259 260 247 261 require_once ABSPATH . 'wp-admin/includes/image.php'; 248 $attach_data = wp_generate_attachment_metadata( $attach_id, $new_file_path);249 wp_update_attachment_metadata( $attach_id, $attach_data);250 251 return array( $attach_id, wp_get_attachment_url( $attach_id )); // Return the image URL.262 $attach_data = wp_generate_attachment_metadata($attach_id, $new_file_path); 263 wp_update_attachment_metadata($attach_id, $attach_data); 264 265 return array($attach_id, wp_get_attachment_url($attach_id)); // Return the image URL. 252 266 } 253 267 … … 263 277 * @return string 264 278 */ 265 private function elevation_blocks_home_page( $uploaded_images ) { 266 if ( empty( $uploaded_images['home-banner'] ) || empty( $uploaded_images['home-second-section'] ) ) { 279 private function elevation_blocks_home_page($uploaded_images) 280 { 281 if (empty($uploaded_images['home-banner']) || empty($uploaded_images['home-second-section'])) { 267 282 return ''; 268 283 } … … 394 409 * @return string 395 410 */ 396 private function elevation_blocks_about_page( $uploaded_images ) { 397 if ( empty( $uploaded_images['about-banner'] ) ) { 411 private function elevation_blocks_about_page($uploaded_images) 412 { 413 if (empty($uploaded_images['about-banner'])) { 398 414 return ''; 399 415 } … … 535 551 * @return string 536 552 */ 537 private function elevation_blocks_contact_page( $uploaded_images ) { 538 if ( empty( $uploaded_images['contact-banner'] ) ) { 553 private function elevation_blocks_contact_page($uploaded_images) 554 { 555 if (empty($uploaded_images['contact-banner'])) { 539 556 return ''; 540 557 } … … 662 679 * @return void 663 680 */ 664 public function elevation_blocks_add_default_widgets_to_sidebars() { 681 public function elevation_blocks_add_default_widgets_to_sidebars() 682 { 665 683 $this->elevation_blocks_update_option_wiget_block(); 666 684 $this->elevation_blocks_update_option_sidebar_widgets(); … … 672 690 * @return void 673 691 */ 674 public function elevation_blocks_update_option_wiget_block() { 692 public function elevation_blocks_update_option_wiget_block() 693 { 675 694 $content_2 = preg_replace( 676 695 '/\s+/', … … 725 744 ); 726 745 727 update_option( 'elevation_blocks_widget_block', $widget_block);746 update_option('elevation_blocks_widget_block', $widget_block); 728 747 } 729 748 … … 733 752 * @return void 734 753 */ 735 public function elevation_blocks_update_option_sidebar_widgets() { 754 public function elevation_blocks_update_option_sidebar_widgets() 755 { 736 756 $sidebar_widgets = array( 737 757 'wp_inactive_widgets' => … … 750 770 ); 751 771 752 update_option( 'elevation_blocks_sidebar_widgets', $sidebar_widgets);772 update_option('elevation_blocks_sidebar_widgets', $sidebar_widgets); 753 773 } 754 774 … … 758 778 * @return void 759 779 */ 760 private function elevation_blocks_set_custom_image() { 780 private function elevation_blocks_set_custom_image() 781 { 761 782 // Define the image URL. 762 783 $header_image_url = ELEVATION_BLOCKS_DIR_URL . '/lib/admin/controls/images/logo-elevation.webp'; … … 764 785 765 786 // Set the image URL programmatically in the customizer. 766 set_theme_mod( 'custom_logo', $header_image_url);767 set_theme_mod( 'footer_logo', $footer_image_url);787 set_theme_mod('custom_logo', $header_image_url); 788 set_theme_mod('footer_logo', $footer_image_url); 768 789 } 769 790 … … 773 794 * @return void 774 795 */ 775 public function elevation_blocks_create_custom_menu() { 796 public function elevation_blocks_create_custom_menu() 797 { 776 798 // Define the menu name. 777 799 $menu_name = 'Main Menu'; 778 800 779 801 // Check if the menu already exists. 780 $menu_exists = wp_get_nav_menu_object( $menu_name);802 $menu_exists = wp_get_nav_menu_object($menu_name); 781 803 782 804 // Create menu if it doesn't exist. 783 if ( ! $menu_exists) {784 $menu_id = wp_create_nav_menu( $menu_name);805 if (! $menu_exists) { 806 $menu_id = wp_create_nav_menu($menu_name); 785 807 786 808 // Add menu items. … … 789 811 0, 790 812 array( 791 'menu-item-title' => esc_html__( 'Home', 'elevation-blocks'),792 'menu-item-url' => home_url( '/'),813 'menu-item-title' => esc_html__('Home', 'elevation-blocks'), 814 'menu-item-url' => home_url('/'), 793 815 'menu-item-status' => 'publish', 794 816 ) … … 799 821 0, 800 822 array( 801 'menu-item-title' => esc_html__( 'About', 'elevation-blocks'),802 'menu-item-url' => home_url( '/about/'),823 'menu-item-title' => esc_html__('About', 'elevation-blocks'), 824 'menu-item-url' => home_url('/about/'), 803 825 'menu-item-status' => 'publish', 804 826 ) … … 809 831 0, 810 832 array( 811 'menu-item-title' => esc_html__( 'Contact', 'elevation-blocks'),812 'menu-item-url' => home_url( '/contact/'),833 'menu-item-title' => esc_html__('Contact', 'elevation-blocks'), 834 'menu-item-url' => home_url('/contact/'), 813 835 'menu-item-status' => 'publish', 814 836 ) … … 816 838 817 839 // Assign menu to a theme location (update with your theme location). 818 $locations = get_theme_mod( 'nav_menu_locations');840 $locations = get_theme_mod('nav_menu_locations'); 819 841 $locations['menu-1'] = $menu_id; // Change 'menu-1' to the correct theme location. 820 set_theme_mod( 'nav_menu_locations', $locations);842 set_theme_mod('nav_menu_locations', $locations); 821 843 } 822 844 } … … 827 849 * @return object 828 850 */ 829 public static function instance() { 830 if ( is_null( self::$instance ) ) { 851 public static function instance() 852 { 853 if (is_null(self::$instance)) { 831 854 self::$instance = new self(); 832 855 } -
elevation-blocks/trunk/lib/admin/controls/class-helpers.php
r3313416 r3313497 1 1 <?php 2 2 3 /** 3 4 * File Name: class-helpers.php … … 10 11 namespace ElevationBlocks\Lib\Admin\Controls; 11 12 12 if ( ! defined( 'ABSPATH' )) {13 if (! defined('ABSPATH')) { 13 14 exit; 14 15 } … … 17 18 * Class Helpers 18 19 */ 19 class Helpers { 20 class Helpers 21 { 20 22 21 23 … … 32 34 * @return void 33 35 */ 34 public function __construct() { 35 add_action( 'admin_menu', array( $this, 'elevation_blocks_import_admin_menu' ) ); 36 public function __construct() 37 { 38 add_action('admin_menu', array($this, 'elevation_blocks_import_admin_menu')); 36 39 } 37 40 … … 41 44 * @return html 42 45 */ 43 public function elevation_blocks_import_page_html() { 44 if ( ! current_user_can( 'manage_options' ) ) { 46 public function elevation_blocks_import_page_html() 47 { 48 if (! current_user_can('manage_options')) { 45 49 return; 46 50 } 47 51 48 52 // Handle import action. 49 if ( isset( $_POST['import_demo_content'] )) {53 if (isset($_POST['import_demo_content'])) { 50 54 // Unslash the data before verification. 51 $import_demo_nonce = isset( $_POST['import_demo_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['import_demo_nonce'] )) : '';55 $import_demo_nonce = isset($_POST['import_demo_nonce']) ? sanitize_text_field(wp_unslash($_POST['import_demo_nonce'])) : ''; 52 56 53 57 // Verify the nonce after unslashing. 54 if ( ! wp_verify_nonce( $import_demo_nonce, 'import_demo_nonce_action' )) {55 wp_die( esc_html__( 'Nonce verification failed', 'elevation-blocks' ));58 if (! wp_verify_nonce($import_demo_nonce, 'import_demo_nonce_action')) { 59 wp_die(esc_html__('Nonce verification failed', 'elevation-blocks')); 56 60 } 57 61 … … 64 68 } 65 69 66 ?>70 ?> 67 71 <div class="wrap"> 68 <h1><?php echo esc_html__( 'Import Elevation Demo Content', 'elevation-blocks'); ?></h1>69 <p><?php echo esc_html__( 'Click the button below to import default pages for your theme.', 'elevation-blocks'); ?></p>72 <h1><?php echo esc_html__('Import Elevation Demo Content', 'elevation-blocks'); ?></h1> 73 <p><?php echo esc_html__('Click the button below to import default pages for your theme.', 'elevation-blocks'); ?></p> 70 74 <form method="post"> 71 <?php wp_nonce_field( 'import_demo_nonce_action', 'import_demo_nonce'); ?>72 <input type="submit" name="import_demo_content" class="button button-primary" value="<?php echo esc_attr__( 'Import Pages', 'elevation-blocks'); ?>" onclick="return confirm('Are you sure you want to import demo content? This could overwrite existing content.');">75 <?php wp_nonce_field('import_demo_nonce_action', 'import_demo_nonce'); ?> 76 <input type="submit" name="import_demo_content" class="button button-primary" value="<?php echo esc_attr__('Import Pages', 'elevation-blocks'); ?>" onclick="return confirm('Are you sure you want to import demo content? This could overwrite existing content.');"> 73 77 </form> 74 78 </div> 75 <?php79 <?php 76 80 } 77 81 … … 81 85 * @return void 82 86 */ 83 public function elevation_blocks_import_admin_menu() { 87 public function elevation_blocks_import_admin_menu() 88 { 84 89 add_management_page( 85 esc_html__( 'Import Elevation Demo Content', 'elevation-blocks'), // Page title.86 esc_html__( 'Import Elevation Demo Content', 'elevation-blocks'), // Menu title.90 esc_html__('Import Elevation Demo Content', 'elevation-blocks'), // Page title. 91 esc_html__('Import Elevation Demo Content', 'elevation-blocks'), // Menu title. 87 92 'manage_options', // Capability. 88 93 'elevation-import', // Menu slug. 89 array( $this, 'elevation_blocks_import_page_html'), // Callback function.94 array($this, 'elevation_blocks_import_page_html'), // Callback function. 90 95 ); 91 96 } … … 96 101 * @return void 97 102 */ 98 public function elevation_blocks_import_demo_pages() { 103 public function elevation_blocks_import_demo_pages() 104 { 99 105 // Define the paths to the images. 100 106 $image_paths = array( 101 'home-banner' => plugin_dir_url( __DIR__ ). 'assets/matthew-henry-2Ts5HnA67k8-unsplash.avif',102 'home-second-section' => plugin_dir_url( __DIR__ ). 'assets/matthew-henry-U5rMrSI7Pn4-unsplash.avif',103 'about-banner' => plugin_dir_url( __DIR__ ). 'assets/paulo-simoes-mendes-V8YzvXKLwDw-unsplash.avif',104 'contact-banner' => plugin_dir_url( __DIR__ ). 'assets/samantha-sophia-NaWKMlp3tVs-unsplash.avif',107 'home-banner' => ELEVATION_BLOCKS_DIR . 'assets/matthew-henry-2Ts5HnA67k8-unsplash.avif', 108 'home-second-section' => ELEVATION_BLOCKS_DIR . 'assets/matthew-henry-U5rMrSI7Pn4-unsplash.avif', 109 'about-banner' => ELEVATION_BLOCKS_DIR . 'assets/paulo-simoes-mendes-V8YzvXKLwDw-unsplash.avif', 110 'contact-banner' => ELEVATION_BLOCKS_DIR . 'assets/samantha-sophia-NaWKMlp3tVs-unsplash.avif', 105 111 ); 106 112 113 107 114 $uploaded_images = array(); 108 foreach ( $image_paths as $key => $image_path) {109 $uploaded_images[ $key ] = $this->elevation_blocks_upload_image( $image_path);115 foreach ($image_paths as $key => $image_path) { 116 $uploaded_images[$key] = $this->elevation_blocks_upload_image($image_path); 110 117 } 111 118 … … 113 120 array( 114 121 'title' => 'Home', 115 'content' => $this->elevation_blocks_home_page( $uploaded_images),122 'content' => $this->elevation_blocks_home_page($uploaded_images), 116 123 ), 117 124 array( 118 125 'title' => 'About', 119 'content' => $this->elevation_blocks_about_page( $uploaded_images),126 'content' => $this->elevation_blocks_about_page($uploaded_images), 120 127 ), 121 128 array( 122 129 'title' => 'Contact', 123 'content' => $this->elevation_blocks_contact_page( $uploaded_images),130 'content' => $this->elevation_blocks_contact_page($uploaded_images), 124 131 ), 125 132 ); 126 133 127 foreach ( $default_pages as $page) {134 foreach ($default_pages as $page) { 128 135 // Check if the page already exists. 129 136 $page_query = new \WP_Query( … … 138 145 $existing_page = $page_query->have_posts(); 139 146 140 if ( ! $existing_page) {147 if (! $existing_page) { 141 148 $page_id = wp_insert_post( 142 149 array( … … 148 155 ); 149 156 150 if ( 'Home' === $page['title']) {151 update_option( 'show_on_front', 'page'); // Set static page mode.152 update_option( 'page_on_front', $page_id); // Set the front page.157 if ('Home' === $page['title']) { 158 update_option('show_on_front', 'page'); // Set static page mode. 159 update_option('page_on_front', $page_id); // Set the front page. 153 160 } 154 161 } … … 163 170 * @return array 164 171 */ 165 private function elevation_blocks_upload_image( $file_path ) { 166 if ( ! file_exists( $file_path ) ) { 172 private function elevation_blocks_upload_image($file_path) 173 { 174 if (! file_exists($file_path)) { 167 175 return false; 168 176 } 169 177 170 $file_name = basename( $file_path);178 $file_name = basename($file_path); 171 179 $upload_dir = wp_upload_dir(); 172 180 $new_file_path = $upload_dir['path'] . '/' . $file_name; 173 181 174 if ( ! copy( $file_path, $new_file_path )) {182 if (! copy($file_path, $new_file_path)) { 175 183 return false; 176 184 } 177 185 178 $file_type = wp_check_filetype( $new_file_path, null);186 $file_type = wp_check_filetype($new_file_path, null); 179 187 $attachment = array( 180 188 'post_mime_type' => $file_type['type'], 181 'post_title' => sanitize_file_name( $file_name),189 'post_title' => sanitize_file_name($file_name), 182 190 'post_content' => '', 183 191 'post_status' => 'inherit', 184 192 ); 185 193 186 $attach_id = wp_insert_attachment( $attachment, $new_file_path);194 $attach_id = wp_insert_attachment($attachment, $new_file_path); 187 195 require_once ABSPATH . 'wp-admin/includes/image.php'; 188 $attach_data = wp_generate_attachment_metadata( $attach_id, $new_file_path);189 wp_update_attachment_metadata( $attach_id, $attach_data);190 191 return array( $attach_id, wp_get_attachment_url( $attach_id )); // Return the image URL.196 $attach_data = wp_generate_attachment_metadata($attach_id, $new_file_path); 197 wp_update_attachment_metadata($attach_id, $attach_data); 198 199 return array($attach_id, wp_get_attachment_url($attach_id)); // Return the image URL. 192 200 } 193 201 … … 199 207 * @return array 200 208 */ 201 private function elevation_blocks_upload_image_from_url( $image_url ) { 202 if ( ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) { 209 private function elevation_blocks_upload_image_from_url($image_url) 210 { 211 212 if (! filter_var($image_url, FILTER_VALIDATE_URL)) { 203 213 return false; 204 214 } 205 215 206 216 // Get file contents. 207 $response = wp_remote_get( $image_url ); 208 209 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { 217 $response = wp_remote_get($image_url); 218 219 220 221 if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { 210 222 return false; 211 223 } 212 224 213 $image_data = wp_remote_retrieve_body( $response);214 if ( empty( $image_data )) {225 $image_data = wp_remote_retrieve_body($response); 226 if (empty($image_data)) { 215 227 return false; 216 228 } 217 229 218 230 // Extract filename from URL. 219 $file_name = basename( wp_parse_url( $image_url, PHP_URL_PATH ));231 $file_name = basename(wp_parse_url($image_url, PHP_URL_PATH)); 220 232 221 233 // Get upload directory. 222 234 $upload_dir = wp_upload_dir(); 223 $new_file_path = trailingslashit( $upload_dir['path']) . $file_name;235 $new_file_path = trailingslashit($upload_dir['path']) . $file_name; 224 236 225 237 // Use WP_Filesystem to save the file. … … 229 241 WP_Filesystem(); 230 242 231 if ( ! $wp_filesystem->put_contents( $new_file_path, $image_data, FS_CHMOD_FILE )) {243 if (! $wp_filesystem->put_contents($new_file_path, $image_data, FS_CHMOD_FILE)) { 232 244 return false; 233 245 } 234 246 235 247 // Get file type. 236 $file_type = wp_check_filetype( $file_name, null);248 $file_type = wp_check_filetype($file_name, null); 237 249 238 250 // Create attachment. 239 251 $attachment = array( 240 252 'post_mime_type' => $file_type['type'], 241 'post_title' => sanitize_file_name( $file_name),253 'post_title' => sanitize_file_name($file_name), 242 254 'post_content' => '', 243 255 'post_status' => 'inherit', 244 256 ); 245 257 246 $attach_id = wp_insert_attachment( $attachment, $new_file_path ); 258 $attach_id = wp_insert_attachment($attachment, $new_file_path); 259 260 247 261 require_once ABSPATH . 'wp-admin/includes/image.php'; 248 $attach_data = wp_generate_attachment_metadata( $attach_id, $new_file_path);249 wp_update_attachment_metadata( $attach_id, $attach_data);250 251 return array( $attach_id, wp_get_attachment_url( $attach_id )); // Return the image URL.262 $attach_data = wp_generate_attachment_metadata($attach_id, $new_file_path); 263 wp_update_attachment_metadata($attach_id, $attach_data); 264 265 return array($attach_id, wp_get_attachment_url($attach_id)); // Return the image URL. 252 266 } 253 267 … … 263 277 * @return string 264 278 */ 265 private function elevation_blocks_home_page( $uploaded_images ) { 266 if ( empty( $uploaded_images['home-banner'] ) || empty( $uploaded_images['home-second-section'] ) ) { 279 private function elevation_blocks_home_page($uploaded_images) 280 { 281 if (empty($uploaded_images['home-banner']) || empty($uploaded_images['home-second-section'])) { 267 282 return ''; 268 283 } … … 394 409 * @return string 395 410 */ 396 private function elevation_blocks_about_page( $uploaded_images ) { 397 if ( empty( $uploaded_images['about-banner'] ) ) { 411 private function elevation_blocks_about_page($uploaded_images) 412 { 413 if (empty($uploaded_images['about-banner'])) { 398 414 return ''; 399 415 } … … 535 551 * @return string 536 552 */ 537 private function elevation_blocks_contact_page( $uploaded_images ) { 538 if ( empty( $uploaded_images['contact-banner'] ) ) { 553 private function elevation_blocks_contact_page($uploaded_images) 554 { 555 if (empty($uploaded_images['contact-banner'])) { 539 556 return ''; 540 557 } … … 662 679 * @return void 663 680 */ 664 public function elevation_blocks_add_default_widgets_to_sidebars() { 681 public function elevation_blocks_add_default_widgets_to_sidebars() 682 { 665 683 $this->elevation_blocks_update_option_wiget_block(); 666 684 $this->elevation_blocks_update_option_sidebar_widgets(); … … 672 690 * @return void 673 691 */ 674 public function elevation_blocks_update_option_wiget_block() { 692 public function elevation_blocks_update_option_wiget_block() 693 { 675 694 $content_2 = preg_replace( 676 695 '/\s+/', … … 725 744 ); 726 745 727 update_option( 'elevation_blocks_widget_block', $widget_block);746 update_option('elevation_blocks_widget_block', $widget_block); 728 747 } 729 748 … … 733 752 * @return void 734 753 */ 735 public function elevation_blocks_update_option_sidebar_widgets() { 754 public function elevation_blocks_update_option_sidebar_widgets() 755 { 736 756 $sidebar_widgets = array( 737 757 'wp_inactive_widgets' => … … 750 770 ); 751 771 752 update_option( 'elevation_blocks_sidebar_widgets', $sidebar_widgets);772 update_option('elevation_blocks_sidebar_widgets', $sidebar_widgets); 753 773 } 754 774 … … 758 778 * @return void 759 779 */ 760 private function elevation_blocks_set_custom_image() { 780 private function elevation_blocks_set_custom_image() 781 { 761 782 // Define the image URL. 762 783 $header_image_url = ELEVATION_BLOCKS_DIR_URL . '/lib/admin/controls/images/logo-elevation.webp'; … … 764 785 765 786 // Set the image URL programmatically in the customizer. 766 set_theme_mod( 'custom_logo', $header_image_url);767 set_theme_mod( 'footer_logo', $footer_image_url);787 set_theme_mod('custom_logo', $header_image_url); 788 set_theme_mod('footer_logo', $footer_image_url); 768 789 } 769 790 … … 773 794 * @return void 774 795 */ 775 public function elevation_blocks_create_custom_menu() { 796 public function elevation_blocks_create_custom_menu() 797 { 776 798 // Define the menu name. 777 799 $menu_name = 'Main Menu'; 778 800 779 801 // Check if the menu already exists. 780 $menu_exists = wp_get_nav_menu_object( $menu_name);802 $menu_exists = wp_get_nav_menu_object($menu_name); 781 803 782 804 // Create menu if it doesn't exist. 783 if ( ! $menu_exists) {784 $menu_id = wp_create_nav_menu( $menu_name);805 if (! $menu_exists) { 806 $menu_id = wp_create_nav_menu($menu_name); 785 807 786 808 // Add menu items. … … 789 811 0, 790 812 array( 791 'menu-item-title' => esc_html__( 'Home', 'elevation-blocks'),792 'menu-item-url' => home_url( '/'),813 'menu-item-title' => esc_html__('Home', 'elevation-blocks'), 814 'menu-item-url' => home_url('/'), 793 815 'menu-item-status' => 'publish', 794 816 ) … … 799 821 0, 800 822 array( 801 'menu-item-title' => esc_html__( 'About', 'elevation-blocks'),802 'menu-item-url' => home_url( '/about/'),823 'menu-item-title' => esc_html__('About', 'elevation-blocks'), 824 'menu-item-url' => home_url('/about/'), 803 825 'menu-item-status' => 'publish', 804 826 ) … … 809 831 0, 810 832 array( 811 'menu-item-title' => esc_html__( 'Contact', 'elevation-blocks'),812 'menu-item-url' => home_url( '/contact/'),833 'menu-item-title' => esc_html__('Contact', 'elevation-blocks'), 834 'menu-item-url' => home_url('/contact/'), 813 835 'menu-item-status' => 'publish', 814 836 ) … … 816 838 817 839 // Assign menu to a theme location (update with your theme location). 818 $locations = get_theme_mod( 'nav_menu_locations');840 $locations = get_theme_mod('nav_menu_locations'); 819 841 $locations['menu-1'] = $menu_id; // Change 'menu-1' to the correct theme location. 820 set_theme_mod( 'nav_menu_locations', $locations);842 set_theme_mod('nav_menu_locations', $locations); 821 843 } 822 844 } … … 827 849 * @return object 828 850 */ 829 public static function instance() { 830 if ( is_null( self::$instance ) ) { 851 public static function instance() 852 { 853 if (is_null(self::$instance)) { 831 854 self::$instance = new self(); 832 855 }
Note: See TracChangeset
for help on using the changeset viewer.