Skip to content

Commit 24bb88c

Browse files
authored
Merge pull request #10 from SolidDigital/feature/custom-css-usage
Feature/custom css usage
2 parents 1de3a6d + 7b3e013 commit 24bb88c

3 files changed

Lines changed: 103 additions & 3 deletions

File tree

admin-pages/custom-css-usage.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
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)