| @@ -1,0 +1,266 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocksThemeBuilder; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Helper as CoreHelper; | |
| 9 | +use ABlocks\Classes\FileUpload; | |
| 10 | +use ABlocks\Assets as CoreAssets; | |
| 11 | + | |
| 12 | +class Assets { | |
| 13 | + | |
| 14 | + public static function init() { | |
| 15 | + $self = new self(); | |
| 16 | + add_filter( 'ablocks/assets/dashboard_scripts_data', array( $self, 'dashboard_scripts_data' ) ); | |
| 17 | + add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 ); | |
| 18 | + } | |
| 19 | + | |
| 20 | + public static function get_post_target_rule_options( $post_type, $taxonomy ) { | |
| 21 | + $post_key = str_replace( ' ', '-', strtolower( $post_type->label ) ); | |
| 22 | + $post_label = ucwords( $post_type->label ); | |
| 23 | + $post_name = $post_type->name; | |
| 24 | + $options = []; | |
| 25 | + | |
| 26 | + $options[] = [ | |
| 27 | + 'value' => $post_name . '|all', | |
| 28 | + /* translators: %s template */ | |
| 29 | + 'label' => sprintf( __( 'All %s', 'ablocks' ), $post_label ), | |
| 30 | + ]; | |
| 31 | + | |
| 32 | + if ( 'pages' !== $post_key ) { | |
| 33 | + $options[] = [ | |
| 34 | + 'value' => $post_name . '|all|archive', | |
| 35 | + /* translators: %s template */ | |
| 36 | + 'label' => sprintf( __( 'All %s Archive', 'ablocks' ), $post_label ), | |
| 37 | + ]; | |
| 38 | + } | |
| 39 | + | |
| 40 | + if ( in_array( $post_type->name, $taxonomy->object_type ) ) { | |
| 41 | + $tax_label = ucwords( $taxonomy->label ); | |
| 42 | + $tax_name = $taxonomy->name; | |
| 43 | + | |
| 44 | + $options[] = [ | |
| 45 | + 'value' => $post_name . '|all|taxarchive|' . $tax_name, | |
| 46 | + /* translators: %s template */ | |
| 47 | + 'label' => sprintf( __( 'All %s Archive', 'ablocks' ), $tax_label ), | |
| 48 | + ]; | |
| 49 | + } | |
| 50 | + | |
| 51 | + return [ | |
| 52 | + 'label' => $post_label, | |
| 53 | + 'options' => $options, | |
| 54 | + ]; | |
| 55 | + } | |
| 56 | + | |
| 57 | + public static function get_location_selections() { | |
| 58 | + $args = [ | |
| 59 | + 'public' => true, | |
| 60 | + '_builtin' => true | |
| 61 | + ]; | |
| 62 | + $post_types = get_post_types( $args, 'objects' ); | |
| 63 | + unset( $post_types['attachment'] ); | |
| 64 | + | |
| 65 | + $args['_builtin'] = false; | |
| 66 | + $custom_post_types = get_post_types( $args, 'objects' ); | |
| 67 | + $post_types = apply_filters( 'ablocks_theme_builder/location_rule_post_types', array_merge( $post_types, $custom_post_types ) ); | |
| 68 | + | |
| 69 | + $groups = []; | |
| 70 | + | |
| 71 | + // Basic group | |
| 72 | + $groups[] = [ | |
| 73 | + 'label' => __( 'Basic', 'ablocks' ), | |
| 74 | + 'options' => [ | |
| 75 | + [ | |
| 76 | + 'value' => 'basic-global', | |
| 77 | + 'label' => __( 'Entire Website', 'ablocks' ) | |
| 78 | + ], | |
| 79 | + [ | |
| 80 | + 'value' => 'basic-singulars', | |
| 81 | + 'label' => __( 'All Singulars', 'ablocks' ) | |
| 82 | + ], | |
| 83 | + [ | |
| 84 | + 'value' => 'basic-archives', | |
| 85 | + 'label' => __( 'All Archives', 'ablocks' ) | |
| 86 | + ], | |
| 87 | + ], | |
| 88 | + ]; | |
| 89 | + | |
| 90 | + // Special Pages group | |
| 91 | + $special_pages = [ | |
| 92 | + [ | |
| 93 | + 'value' => 'special-404', | |
| 94 | + 'label' => __( '404 Page', 'ablocks' ) | |
| 95 | + ], | |
| 96 | + [ | |
| 97 | + 'value' => 'special-search', | |
| 98 | + 'label' => __( 'Search Page', 'ablocks' ) | |
| 99 | + ], | |
| 100 | + [ | |
| 101 | + 'value' => 'special-blog', | |
| 102 | + 'label' => __( 'Blog / Posts Page', 'ablocks' ) | |
| 103 | + ], | |
| 104 | + [ | |
| 105 | + 'value' => 'special-front', | |
| 106 | + 'label' => __( 'Front Page', 'ablocks' ) | |
| 107 | + ], | |
| 108 | + [ | |
| 109 | + 'value' => 'special-date', | |
| 110 | + 'label' => __( 'Date Archive', 'ablocks' ) | |
| 111 | + ], | |
| 112 | + [ | |
| 113 | + 'value' => 'special-author', | |
| 114 | + 'label' => __( 'Author Archive', 'ablocks' ) | |
| 115 | + ], | |
| 116 | + ]; | |
| 117 | + | |
| 118 | + if ( class_exists( 'WooCommerce' ) ) { | |
| 119 | + $special_pages[] = [ | |
| 120 | + 'value' => 'special-woo-shop', | |
| 121 | + 'label' => __( 'WooCommerce Shop Page', 'ablocks' ) | |
| 122 | + ]; | |
| 123 | + } | |
| 124 | + | |
| 125 | + $groups[] = [ | |
| 126 | + 'label' => __( 'Special Pages', 'ablocks' ), | |
| 127 | + 'options' => $special_pages, | |
| 128 | + ]; | |
| 129 | + | |
| 130 | + // Custom post types and taxonomies | |
| 131 | + $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' ); | |
| 132 | + foreach ( $taxonomies as $taxonomy ) { | |
| 133 | + if ( 'post_format' === $taxonomy->name ) { | |
| 134 | + continue; | |
| 135 | + } | |
| 136 | + | |
| 137 | + foreach ( $post_types as $post_type ) { | |
| 138 | + if ( $post_type === 'ablocks_tb' ) { | |
| 139 | + continue; | |
| 140 | + } | |
| 141 | + $group = self::get_post_target_rule_options( $post_type, $taxonomy ); | |
| 142 | + | |
| 143 | + // Avoid duplicate labels | |
| 144 | + $existing_group = array_filter( $groups, fn( $g) => $g['label'] === $group['label'] ); | |
| 145 | + if ( ! empty( $existing_group ) ) { | |
| 146 | + foreach ( $groups as &$g ) { | |
| 147 | + if ( $g['label'] === $group['label'] ) { | |
| 148 | + $merged = array_merge( $g['options'], $group['options'] ); | |
| 149 | + | |
| 150 | + // Remove duplicate options by unique 'value' field (or 'label', depending on your use case) | |
| 151 | + $unique = []; | |
| 152 | + foreach ( $merged as $option ) { | |
| 153 | + $unique_key = $option['value']; // or use $option['label'] if needed | |
| 154 | + $unique[ $unique_key ] = $option; | |
| 155 | + } | |
| 156 | + | |
| 157 | + $g['options'] = array_values( $unique ); | |
| 158 | + } | |
| 159 | + } | |
| 160 | + } else { | |
| 161 | + $groups[] = $group; | |
| 162 | + } | |
| 163 | + }//end foreach | |
| 164 | + }//end foreach | |
| 165 | + | |
| 166 | + // Specific Target group | |
| 167 | + $groups[] = [ | |
| 168 | + 'label' => __( 'Specific Target', 'ablocks' ), | |
| 169 | + 'options' => [ | |
| 170 | + [ | |
| 171 | + 'value' => 'specifics', | |
| 172 | + 'label' => __( 'Specific Pages / Posts / Taxonomies, etc.', 'ablocks' ) | |
| 173 | + ], | |
| 174 | + ], | |
| 175 | + ]; | |
| 176 | + | |
| 177 | + return apply_filters( 'ablocks/theme_builder/display_location_lists', $groups ); | |
| 178 | + } | |
| 179 | + | |
| 180 | + public static function get_user_selections() { | |
| 181 | + $groups = []; | |
| 182 | + | |
| 183 | + // Basic group | |
| 184 | + $groups[] = [ | |
| 185 | + 'label' => __( 'Basic', 'ablocks' ), | |
| 186 | + 'options' => [ | |
| 187 | + [ | |
| 188 | + 'value' => 'all', | |
| 189 | + 'label' => __( 'All', 'ablocks' ) | |
| 190 | + ], | |
| 191 | + [ | |
| 192 | + 'value' => 'logged-in', | |
| 193 | + 'label' => __( 'Logged In', 'ablocks' ) | |
| 194 | + ], | |
| 195 | + [ | |
| 196 | + 'value' => 'logged-out', | |
| 197 | + 'label' => __( 'Logged Out', 'ablocks' ) | |
| 198 | + ], | |
| 199 | + ], | |
| 200 | + ]; | |
| 201 | + | |
| 202 | + // Advanced group | |
| 203 | + $roles = get_editable_roles(); | |
| 204 | + $role_options = []; | |
| 205 | + | |
| 206 | + foreach ( $roles as $slug => $data ) { | |
| 207 | + $role_options[] = [ | |
| 208 | + 'value' => $slug, | |
| 209 | + 'label' => $data['name'], | |
| 210 | + ]; | |
| 211 | + } | |
| 212 | + | |
| 213 | + $groups[] = [ | |
| 214 | + 'label' => __( 'Advanced', 'ablocks' ), | |
| 215 | + 'options' => $role_options, | |
| 216 | + ]; | |
| 217 | + | |
| 218 | + return apply_filters( 'ablocks/theme_builder/user_roles', $groups ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + public function dashboard_scripts_data( $data ) { | |
| 222 | + $data['theme_builder'] = [ | |
| 223 | + 'locations' => $this->get_location_selections(), | |
| 224 | + 'roles' => $this->get_user_selections(), | |
| 225 | + ]; | |
| 226 | + return $data; | |
| 227 | + } | |
| 228 | + | |
| 229 | + public function enqueue_frontend_assets() { | |
| 230 | + if ( ! CoreHelper::is_enabled_assets_generation() ) { | |
| 231 | + return; | |
| 232 | + } | |
| 233 | + if ( ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) ) { | |
| 234 | + return; | |
| 235 | + } | |
| 236 | + | |
| 237 | + $template_types = [ | |
| 238 | + 'header', | |
| 239 | + 'footer' | |
| 240 | + ]; | |
| 241 | + | |
| 242 | + $FileUpload = new FileUpload(); | |
| 243 | + foreach ( $template_types as $template_type ) { | |
| 244 | + $post_id = Helper::get_template_id( $template_type ); | |
| 245 | + if ( $post_id ) { | |
| 246 | + $css_file_path = $FileUpload->get_file_path( $post_id . '.min.css' ); | |
| 247 | + if ( file_exists( $css_file_path ) ) { | |
| 248 | + $css_file_url = $FileUpload->get_file_url( $post_id . '.min.css' ); | |
| 249 | + // Enqueue google fonts | |
| 250 | + wp_enqueue_style( 'ablocks-frontend-google-fonts' ); | |
| 251 | + // Combine CSS | |
| 252 | + wp_enqueue_style( 'ablocks-blocks-' . $template_type . '-' . $post_id . '-combine-style', $css_file_url, array(), filemtime( $css_file_path ), 'all' ); | |
| 253 | + } | |
| 254 | + $js_file_path = $FileUpload->get_file_path( $post_id . '.min.js' ); | |
| 255 | + if ( file_exists( $js_file_path ) ) { | |
| 256 | + $js_file_url = $FileUpload->get_file_url( $post_id . '.min.js' ); | |
| 257 | + // One shared ABlocksGlobal for the whole page: without this each | |
| 258 | + // template type (header/footer/single/archive) printed its own | |
| 259 | + // identical copy on top of the page combine script's. | |
| 260 | + wp_enqueue_script( 'ablocks-blocks-' . $template_type . '-' . $post_id . '-combine-script', $js_file_url, array( 'ablocks-globals' ), filemtime( $js_file_path ), true ); | |
| 261 | + CoreAssets::localize_globals_once(); | |
| 262 | + } | |
| 263 | + } | |
| 264 | + }//end foreach | |
| 265 | + } | |
| 266 | +} | |