1+ <?php
2+ namespace Solid ;
3+
4+ // Hook to add the admin menu page
5+ add_action ('admin_menu ' , function () {
6+ add_submenu_page (
7+ 'solid-dynamics-settings ' ,
8+ __ ('Custom CSS Usage ' , 'solid-dynamics ' ), // Page title
9+ __ ('Custom CSS Usage ' , 'solid-dynamics ' ), // Menu title
10+ 'manage_options ' , // Capability
11+ 'solid-dynamics-custom-css-usage ' , // Menu slug
12+ __NAMESPACE__ . '\render_custom_css_usage_page ' // Callback function
13+ );
14+ });
15+
16+ // Function to render the admin page
17+ function render_custom_css_usage_page () {
18+ $ kit_id = \Elementor \Plugin::$ instance ->kits_manager ->get_active_id ();
19+
20+ $ settings = get_post_meta ( $ kit_id , '_elementor_page_settings ' , true );
21+ $ site_settings_custom_css_line_count = substr_count ( $ settings ['custom_css ' ] ?? "" , "\n" );
22+
23+ $ results = get_all_elementor_custom_css ();
24+ ?>
25+ <div class="wrap">
26+
27+ <h1>Find Elementor Custom CSS Usage</h1>
28+
29+ <?php
30+
31+ if (empty ($ results )) {
32+ echo '<p> ' . __ ('No posts contain custom css ' , 'solid-dynamics ' ) . '.</p> ' ;
33+ return ;
34+ }
35+
36+ echo '<p> ' . __ ('Total Lines in Site Settings Custom CSS: ' , 'solid-dynamics ' ) . $ site_settings_custom_css_line_count . '</p> ' ;
37+ echo '<h2> ' . __ ('Posts ' , 'solid-dynamics ' ) . ':</h2> ' ;
38+ echo '<table class="widefat fixed" cellspacing="0"> ' ;
39+ echo '<thead><tr><th>ID</th><th> ' . __ ('Title ' , 'solid-dynamics ' ) . '</th><th> ' . __ ('Post Type ' , 'solid-dynamics ' ) . '</th><th> ' . __ ('Status ' , 'solid-dynamics ' ) . '</th><th> ' . __ ('Widgets ' , 'solid-dynamics ' ) . '</th></tr></thead> ' ;
40+ echo '<tbody> ' ;
41+
42+ foreach ($ results as $ result ) {
43+ $ edit_link = get_edit_post_link ($ result ['ID ' ]);
44+ echo '<tr> ' ;
45+ echo '<td> ' . $ result ['ID ' ] . '</td> ' ; // Display the instance number
46+ echo '<td><a href=" ' . esc_url ($ edit_link ) . '"> ' . esc_html ($ result ['post_title ' ]) . '</a></td> ' ;
47+ echo '<td> ' . esc_html ($ result ['post_type ' ]) . '</td> ' ;
48+ echo '<td> ' . esc_html ($ result ['post_status ' ]) . '</td> ' ;
49+ echo '<td> ' . esc_html (implode (', ' , $ result ['widgets ' ])) . '</td> ' ;
50+ echo '</tr> ' ;
51+ }
52+
53+ echo '</tbody></table> ' ;
54+
55+ ?>
56+ </div>
57+ <?php
58+ }
59+
60+ // Function to get all Elementor widgets
61+ function get_all_elementor_custom_css () {
62+ global $ wpdb ;
63+
64+ $ query = $ wpdb ->prepare (
65+ "SELECT ID, post_title, post_type, post_status, meta_value as elementor_data
66+ FROM $ wpdb ->postmeta
67+ JOIN $ wpdb ->posts ON $ wpdb ->posts .ID = $ wpdb ->postmeta .post_id
68+ WHERE meta_key = '_elementor_data'
69+ AND meta_value LIKE '%custom_css%'
70+ AND post_status = 'publish' "
71+ );
72+
73+ $ posts = $ wpdb ->get_results ($ query , ARRAY_A );
74+
75+ foreach ($ posts as &$ post ) {
76+ $ elementor_data = json_decode ($ post ['elementor_data ' ], true );
77+
78+ if (is_array ($ elementor_data )) {
79+ $ post ['widgets ' ] = collect_widgets_with_custom_css ($ elementor_data );
80+ }
81+ }
82+
83+ return $ posts ;
84+ }
85+
86+ function collect_widgets_with_custom_css ($ elementor_data , &$ results = []) {
87+
88+ foreach ($ elementor_data as $ widget ) {
89+ if (isset ($ widget ['settings ' ]['custom_css ' ])) {
90+ $ results [] = $ widget ['widgetType ' ] ?? $ widget ['elType ' ];
91+ }
92+
93+ if (isset ($ widget ['elements ' ]) && is_array ($ widget ['elements ' ])) {
94+ collect_widgets_with_custom_css ($ widget ['elements ' ], $ results );
95+ }
96+ }
97+
98+ return $ results ;
99+ }
0 commit comments