Skip to content

Commit 0c329c8

Browse files
committed
custom css usage listed
1 parent 1de3a6d commit 0c329c8

3 files changed

Lines changed: 97 additions & 3 deletions

File tree

admin-pages/custom-css-usage.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
$results = get_all_elementor_custom_css();
19+
20+
?>
21+
<div class="wrap">
22+
<h1>Find Elementor Custom CSS Usage</h1>
23+
24+
<?php
25+
26+
if (empty($results)) {
27+
echo '<p>' . __('No posts contain custom css', 'solid-dynamics') . '.</p>';
28+
return;
29+
}
30+
31+
echo '<h2>' . __('Results', 'solid-dynamics') . ':</h2>';
32+
echo '<table class="widefat fixed" cellspacing="0">';
33+
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>';
34+
echo '<tbody>';
35+
36+
foreach ($results as $result) {
37+
$edit_link = get_edit_post_link($result['ID']);
38+
echo '<tr>';
39+
echo '<td>' . $result['ID'] . '</td>'; // Display the instance number
40+
echo '<td><a href="' . esc_url($edit_link) . '">' . esc_html($result['post_title']) . '</a></td>';
41+
echo '<td>' . esc_html($result['post_type']) . '</td>';
42+
echo '<td>' . esc_html($result['post_status']) . '</td>';
43+
echo '<td>' . esc_html(implode(', ', $result['widgets'])) . '</td>';
44+
echo '</tr>';
45+
}
46+
47+
echo '</tbody></table>';
48+
49+
?>
50+
</div>
51+
<?php
52+
}
53+
54+
// Function to get all Elementor widgets
55+
function get_all_elementor_custom_css() {
56+
global $wpdb;
57+
58+
$query = $wpdb->prepare(
59+
"SELECT ID, post_title, post_type, post_status, meta_value as elementor_data
60+
FROM $wpdb->postmeta
61+
JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->postmeta.post_id
62+
WHERE meta_key = '_elementor_data'
63+
AND meta_value LIKE '%custom_css%'
64+
AND post_status = 'publish'"
65+
);
66+
67+
$posts = $wpdb->get_results($query, ARRAY_A);
68+
69+
foreach ($posts as &$post) {
70+
$elementor_data = json_decode($post['elementor_data'], true);
71+
72+
if (is_array($elementor_data)) {
73+
$post['widgets'] = collect_widgets_with_custom_css($elementor_data);
74+
}
75+
}
76+
77+
return $posts;
78+
}
79+
80+
function collect_widgets_with_custom_css($elementor_data, &$results = []) {
81+
82+
foreach ($elementor_data as $widget) {
83+
if (isset($widget['settings']['custom_css'])) {
84+
$results[] = $widget['widgetType'] ?? $widget['elType'];
85+
}
86+
87+
if (isset($widget['elements']) && is_array($widget['elements'])) {
88+
collect_widgets_with_custom_css($widget['elements'], $results);
89+
}
90+
}
91+
92+
return $results;
93+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
add_action('admin_menu', function () {
66
add_submenu_page(
77
'solid-dynamics-settings',
8-
__('Elementor Widget Usage', 'solid-dynamics'), // Page title
8+
__('Widget Usage', 'solid-dynamics'), // Page title
99
__('Widget Usage', 'solid-dynamics'), // Menu title
1010
'manage_options', // Capability
11-
'elementor-widget-usage', // Menu slug
11+
'solid-dynamics-widget-usage', // Menu slug
1212
__NAMESPACE__ . '\render_widget_usage_page' // Callback function
1313
);
1414
});

solid-dynamics.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@
3434

3535
new Settings();
3636

37-
require_once( __DIR__ . "/widget-usage/admin-page.php" );
37+
require_once( __DIR__ . "/admin-pages/widget-usage.php" );
38+
require_once( __DIR__ . "/admin-pages/custom-css-usage.php" );

0 commit comments

Comments
 (0)