PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.0.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.0.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / core / class-asset-optimizer.php

class-asset-optimizer.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.0.0, at includes/core/class-asset-optimizer.php

139 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Asset Optimizer Class
4 *
5 * Simple asset optimization for better performance.
6 * Implements preloading and basic optimization strategies.
7 *
8 * @package ThinkRank
9 * @subpackage Core
10 * @since 1.0.0
11 */
12
13 declare(strict_types=1);
14
15 namespace ThinkRank\Core;
16
17 /**
18 * Asset Optimizer Class
19 *
20 * @since 1.0.0
21 */
22 class Asset_Optimizer {
23
24 /**
25 * Initialize asset optimization
26 *
27 * @since 1.0.0
28 */
29 public function __construct() {
30 add_action('admin_init', [$this, 'init_admin_optimizations']);
31 }
32
33 /**
34 * Initialize admin-specific optimizations
35 *
36 * @since 1.0.0
37 */
38 public function init_admin_optimizations(): void {
39 // Only optimize on ThinkRank admin pages
40 if (!$this->is_thinkrank_admin_page()) {
41 return;
42 }
43
44 add_action('admin_head', [$this, 'add_asset_preloading'], 1);
45 add_action('admin_head', [$this, 'add_performance_hints'], 2);
46 }
47
48 /**
49 * Check if current page is a ThinkRank admin page
50 *
51 * @since 1.0.0
52 *
53 * @return bool True if ThinkRank admin page
54 */
55 private function is_thinkrank_admin_page(): bool {
56 $screen = get_current_screen();
57
58 if (!$screen) {
59 return false;
60 }
61
62 // Check for ThinkRank admin pages
63 return strpos($screen->id, 'thinkrank') !== false ||
64 strpos($screen->base, 'thinkrank') !== false ||
65 (isset($_GET['page']) && strpos($_GET['page'], 'thinkrank') !== false);
66 }
67
68 /**
69 * Add asset preloading headers
70 *
71 * @since 1.0.0
72 */
73 public function add_asset_preloading(): void {
74 $plugin_url = plugin_dir_url(THINKRANK_PLUGIN_FILE);
75
76 $assets = [
77 // Critical CSS
78 [
79 'url' => $plugin_url . 'assets/admin.css',
80 'type' => 'style'
81 ],
82 // Critical JS
83 [
84 'url' => $plugin_url . 'assets/admin.js',
85 'type' => 'script'
86 ]
87 ];
88
89 foreach ($assets as $asset) {
90 // Check if file exists before preloading
91 $file_path = str_replace($plugin_url, THINKRANK_PLUGIN_DIR, $asset['url']);
92 if (file_exists($file_path)) {
93 printf(
94 '<link rel="preload" href="%s" as="%s"%s>' . "\n",
95 esc_url($asset['url']),
96 esc_attr($asset['type']),
97 $asset['type'] === 'style' ? ' type="text/css"' : ''
98 );
99 }
100 }
101 }
102
103 /**
104 * Add performance hints
105 *
106 * @since 1.0.0
107 */
108 public function add_performance_hints(): void {
109 // DNS prefetch for external APIs (if configured)
110 $external_domains = [
111 'api.openai.com',
112 'api.anthropic.com',
113 'generativelanguage.googleapis.com'
114 ];
115
116 foreach ($external_domains as $domain) {
117 printf('<link rel="dns-prefetch" href="//%s">' . "\n", esc_attr($domain));
118 }
119
120 // Remove redundant viewport meta tag: WordPress admin already sets this.
121 // Keeping only DNS prefetch hints here.
122 }
123
124 /**
125 * Get asset optimization stats
126 *
127 * @since 1.0.0
128 *
129 * @return array Optimization statistics
130 */
131 public function get_stats(): array {
132 return [
133 'preloading_enabled' => $this->is_thinkrank_admin_page(),
134 'dns_prefetch_domains' => 3,
135 'optimized_pages' => ['thinkrank admin pages']
136 ];
137 }
138 }
139