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