| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
use Depicter; |
| 6 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 7 |
|
| 8 |
class UsageService { |
| 9 |
|
| 10 |
/** |
| 11 |
* Get collected data |
| 12 |
* |
| 13 |
* @return array|mixed |
| 14 |
*/ |
| 15 |
public function get() { |
| 16 |
return \Depicter::options()->get( 'usage', ''); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Check if user agrees with collect data consent or not |
| 21 |
* |
| 22 |
* @return bool |
| 23 |
*/ |
| 24 |
public function userAllowedToCollectData() { |
| 25 |
$dataCollectionConsent = Depicter::options()->get( 'data_collect_consent', ''); |
| 26 |
return ! empty( $dataCollectionConsent ) && $dataCollectionConsent == 'allow'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Collect data |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function collect() { |
| 35 |
if ( ! $this->userAllowedToCollectData() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$documents = \Depicter::documentRepository()->document()->where('parent', 0 )->get(); |
| 40 |
if ( empty( $documents ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
$documents = $documents->toArray(); |
| 44 |
|
| 45 |
$documentTypes = []; |
| 46 |
$importantElements = [ |
| 47 |
'"type":"dpcCouponBox"' => 'couponBox', |
| 48 |
'"type":"form"' => 'form', |
| 49 |
'"type":"wpShortcode"' => 'shortcode', |
| 50 |
'"type":"dpcIframe"' => 'Iframe', |
| 51 |
'"type":"dpcCountdown"' => 'countDown', |
| 52 |
'"datasource"' => 'dataSource' |
| 53 |
]; |
| 54 |
|
| 55 |
$importantElementsCount = [ |
| 56 |
'couponBox' => 0, |
| 57 |
'form' => 0, |
| 58 |
'shortcode' => 0, |
| 59 |
'Iframe' => 0, |
| 60 |
'countDown' => 0, |
| 61 |
'dataSource' => 0 |
| 62 |
]; |
| 63 |
|
| 64 |
if ( !empty( $documents ) ) { |
| 65 |
foreach ($documents as $document) { |
| 66 |
$document['type'] = $document['type'] === 'custom' ? 'slider' : $document['type']; |
| 67 |
if ( isset ( $documentTypes[ $document['type'] ] ) ) { |
| 68 |
$documentTypes[ $document['type'] ] += 1; |
| 69 |
} else { |
| 70 |
$documentTypes[ $document['type'] ] = 1; |
| 71 |
} |
| 72 |
|
| 73 |
foreach( $importantElements as $pattern => $element ) { |
| 74 |
$importantElementsCount[ $element ] = $importantElementsCount[ $element ] + substr_count( $document['content'], $pattern ); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$importantElementsCount = array_filter( $importantElementsCount ); |
| 80 |
|
| 81 |
$pageBuilders = [ |
| 82 |
'wpBakery' => 'Vc_Manager', |
| 83 |
'elementor' => '\Elementor\Plugin', |
| 84 |
'divi' => 'ET_Builder_Plugin', |
| 85 |
'beaverBuilder' => '\FLBuilder', |
| 86 |
'oxygen' => '\OxyEl', |
| 87 |
'bricks' => '\Bricks\Elements' |
| 88 |
]; |
| 89 |
|
| 90 |
foreach( $pageBuilders as $pageBuilder => $condition ) { |
| 91 |
if ( ! class_exists( $condition ) ) { |
| 92 |
unset( $pageBuilders[ $pageBuilder ] ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
$leadPlugins = [ |
| 97 |
'wpforms-lite' => '\WPForms\WPForms', |
| 98 |
'optinmonster' => '\OMAPI', |
| 99 |
'popup-maker' => '\Popup_Maker', |
| 100 |
'popup-builder' => '\sgpb\PopupBuilderInit', |
| 101 |
'smart-slider-3' => '\Nextend\SmartSlider3\Platform\SmartSlider3Platform' |
| 102 |
]; |
| 103 |
|
| 104 |
foreach( $leadPlugins as $plugin => $condition ) { |
| 105 |
if ( ! class_exists( $condition ) ) { |
| 106 |
unset( $leadPlugins[ $plugin ] ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
$usage = [ |
| 111 |
'document_stat' => count( $documents ), |
| 112 |
'document_types' => $documentTypes, |
| 113 |
'important_elements' => $importantElementsCount, |
| 114 |
'document_features' => '', |
| 115 |
'wp_tools' => [ |
| 116 |
'themeName' => wp_get_theme()->get( 'Name' ), |
| 117 |
'pageBuilders' => $pageBuilders, |
| 118 |
'leadPlugins' => $leadPlugins |
| 119 |
] |
| 120 |
]; |
| 121 |
|
| 122 |
\Depicter::options()->set( 'usage', $usage ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @throws GuzzleException |
| 127 |
*/ |
| 128 |
public function send() { |
| 129 |
if ( ! $this->userAllowedToCollectData() ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
$usage = Depicter::options()->get( 'usage', []); |
| 134 |
Depicter::remote()->post( '/v1/usage/collect', [ |
| 135 |
'form_params' => [ |
| 136 |
'collected_data' => JSON::encode( $usage ), |
| 137 |
] |
| 138 |
]); |
| 139 |
} |
| 140 |
} |
| 141 |
|