| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Usage; |
| 3 |
|
| 4 |
use Elementor\Modules\System_Info\Reporters\Base; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Elementor usage report. |
| 12 |
* |
| 13 |
* Elementor system report handler class responsible for generating a report for |
| 14 |
* the user. |
| 15 |
*/ |
| 16 |
class Usage_Reporter extends Base { |
| 17 |
|
| 18 |
const RECALC_ACTION = 'elementor_usage_recalc'; |
| 19 |
|
| 20 |
public function get_title() { |
| 21 |
return esc_html__( 'Elements Usage', 'elementor' ); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_fields() { |
| 25 |
return [ |
| 26 |
'usage' => '', |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
public function print_html_label( $label ) { |
| 31 |
$title = $this->get_title(); |
| 32 |
|
| 33 |
if ( empty( $_GET[ self::RECALC_ACTION ] ) ) { // phpcs:ignore -- nonce validation is not required here. |
| 34 |
$nonce = wp_create_nonce( self::RECALC_ACTION ); |
| 35 |
$url = add_query_arg( [ |
| 36 |
self::RECALC_ACTION => 1, |
| 37 |
'_wpnonce' => $nonce, |
| 38 |
] ); |
| 39 |
|
| 40 |
$title .= '<a id="elementor-usage-recalc" href="' . esc_url( $url ) . '#elementor-usage-recalc" class="box-title-tool">Recalculate</a>'; |
| 41 |
} else { |
| 42 |
$title .= $this->get_remove_recalc_query_string_script(); |
| 43 |
} |
| 44 |
|
| 45 |
parent::print_html_label( $title ); |
| 46 |
} |
| 47 |
|
| 48 |
public function get_usage() { |
| 49 |
/** @var Module $module */ |
| 50 |
$module = Module::instance(); |
| 51 |
|
| 52 |
if ( ! empty( $_GET[ self::RECALC_ACTION ] ) ) { |
| 53 |
if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], self::RECALC_ACTION ) ) { |
| 54 |
wp_die( 'Invalid Nonce', 'Invalid Nonce', [ |
| 55 |
'back_link' => true, |
| 56 |
] ); |
| 57 |
} |
| 58 |
|
| 59 |
$module->recalc_usage(); |
| 60 |
} |
| 61 |
|
| 62 |
$usage = ''; |
| 63 |
|
| 64 |
foreach ( $module->get_formatted_usage() as $doc_type => $data ) { |
| 65 |
$usage .= '<tr><td>' . $data['title'] . ' ( ' . $data['count'] . ' )</td><td>'; |
| 66 |
|
| 67 |
foreach ( $data['elements'] as $element => $count ) { |
| 68 |
$usage .= $element . ': ' . $count . PHP_EOL; |
| 69 |
} |
| 70 |
|
| 71 |
$usage .= '</td></tr>'; |
| 72 |
} |
| 73 |
|
| 74 |
return [ |
| 75 |
'value' => $usage, |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
public function get_raw_usage() { |
| 80 |
/** @var Module $module */ |
| 81 |
$module = Module::instance(); |
| 82 |
$usage = PHP_EOL; |
| 83 |
|
| 84 |
foreach ( $module->get_formatted_usage( 'raw' ) as $doc_type => $data ) { |
| 85 |
$usage .= "\t{$data['title']} : " . $data['count'] . PHP_EOL; |
| 86 |
|
| 87 |
foreach ( $data['elements'] as $element => $count ) { |
| 88 |
$usage .= "\t\t{$element} : {$count}" . PHP_EOL; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return [ |
| 93 |
'value' => $usage, |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Removes the "elementor_usage_recalc" param from the query string to avoid recalc every refresh. |
| 99 |
* When using a redirect header in place of this approach it throws an error because some components have already output some content. |
| 100 |
* |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
private function get_remove_recalc_query_string_script() { |
| 104 |
ob_start(); |
| 105 |
?> |
| 106 |
<script> |
| 107 |
// Origin file: modules/usage/usage-reporter.php - get_remove_recalc_query_string_script() |
| 108 |
{ |
| 109 |
const url = new URL( window.location ); |
| 110 |
|
| 111 |
url.hash = ''; |
| 112 |
url.searchParams.delete( 'elementor_usage_recalc' ); |
| 113 |
url.searchParams.delete( '_wpnonce' ); |
| 114 |
|
| 115 |
history.replaceState( '', window.title, url.toString() ); |
| 116 |
} |
| 117 |
</script> |
| 118 |
<?php |
| 119 |
|
| 120 |
return ob_get_clean(); |
| 121 |
} |
| 122 |
} |
| 123 |
|