Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/extendif.../app
File: Insights.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Insights setup
[3] Fix | Delete
*/
[4] Fix | Delete
[5] Fix | Delete
namespace Extendify;
[6] Fix | Delete
[7] Fix | Delete
defined('ABSPATH') || die('No direct access.');
[8] Fix | Delete
[9] Fix | Delete
use Extendify\Shared\Services\Sanitizer;
[10] Fix | Delete
use Extendify\PartnerData;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Controller for handling various Insights related things.
[14] Fix | Delete
* WP code reviewers: This is used in another plugin and not invoked here.
[15] Fix | Delete
*/
[16] Fix | Delete
[17] Fix | Delete
class Insights
[18] Fix | Delete
{
[19] Fix | Delete
/**
[20] Fix | Delete
* An array of active tests. 'A' should be the control.
[21] Fix | Delete
* For weighted tests, try ['A', 'A', 'A', 'A', 'B']
[22] Fix | Delete
*
[23] Fix | Delete
* @var array
[24] Fix | Delete
*/
[25] Fix | Delete
protected $activeTests = [];
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Process the readme file to get version and name
[29] Fix | Delete
*
[30] Fix | Delete
* @return void
[31] Fix | Delete
*/
[32] Fix | Delete
public function __construct()
[33] Fix | Delete
{
[34] Fix | Delete
// If there isn't a siteId, then create one.
[35] Fix | Delete
if (!\get_option('extendify_site_id', false)) {
[36] Fix | Delete
\update_option('extendify_site_id', \wp_generate_uuid4());
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
if (
[40] Fix | Delete
defined('EXTENDIFY_INSIGHTS_URL')
[41] Fix | Delete
&& class_exists('ExtendifyInsights')
[42] Fix | Delete
&& !\get_option('extendify_insights_checkedin_once', 0)
[43] Fix | Delete
) {
[44] Fix | Delete
\update_option('extendify_insights_checkedin_once', gmdate('Y-m-d H:i:s'));
[45] Fix | Delete
// WP code reviewers: This job is defined in another plugin (i.e. it's opt-in).
[46] Fix | Delete
\add_action('init', function () {
[47] Fix | Delete
// Run this once but wait 10 minutes.
[48] Fix | Delete
\wp_schedule_single_event((time() + 10 * MINUTE_IN_SECONDS), 'extendify_insights');
[49] Fix | Delete
\spawn_cron();
[50] Fix | Delete
});
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
$this->setUpActiveTests();
[54] Fix | Delete
$this->filterExternalInsights();
[55] Fix | Delete
$this->setupAdminLoginInsights();
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Returns the active tests for the user, and sets up tests as needed.
[60] Fix | Delete
*
[61] Fix | Delete
* @return void
[62] Fix | Delete
*/
[63] Fix | Delete
public function setUpActiveTests()
[64] Fix | Delete
{
[65] Fix | Delete
// Make sure that the active tests are set.
[66] Fix | Delete
$currentTests = \get_option('extendify_active_tests', []);
[67] Fix | Delete
$newTests = array_map(function ($test) {
[68] Fix | Delete
// Pick from value randomly.
[69] Fix | Delete
return $test[array_rand($test)];
[70] Fix | Delete
}, array_diff_key($this->activeTests, $currentTests));
[71] Fix | Delete
$testsCombined = array_merge($currentTests, $newTests);
[72] Fix | Delete
if ($newTests) {
[73] Fix | Delete
\update_option('extendify_active_tests', Sanitizer::sanitizeArray($testsCombined));
[74] Fix | Delete
}
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* Add additional data to the opt-in insights
[79] Fix | Delete
*
[80] Fix | Delete
* @return void
[81] Fix | Delete
*/
[82] Fix | Delete
public function filterExternalInsights()
[83] Fix | Delete
{
[84] Fix | Delete
add_filter('extendify_insights_data', function ($data) {
[85] Fix | Delete
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
[86] Fix | Delete
$readme = file_get_contents(EXTENDIFY_PATH . 'readme.txt');
[87] Fix | Delete
preg_match('/Stable tag: ([0-9.:]+)/', $readme, $version);
[88] Fix | Delete
[89] Fix | Delete
$insights = array_merge($data, [
[90] Fix | Delete
'launch' => Config::$showLaunch,
[91] Fix | Delete
'launchRedirectedAt' => \get_option('extendify_attempted_redirect', null),
[92] Fix | Delete
'launchLoadedAt' => \get_option('extendify_launch_loaded', null),
[93] Fix | Delete
'partner' => defined('EXTENDIFY_PARTNER_ID') ? constant('EXTENDIFY_PARTNER_ID') : null,
[94] Fix | Delete
'siteCreatedAt' => SiteSettings::getSiteCreatedAt(),
[95] Fix | Delete
'assistRouterData' => \get_option('extendify_assist_router', null),
[96] Fix | Delete
'libraryData' => \get_option('extendify_library_site_data', null),
[97] Fix | Delete
'draftSettingsData' => \get_option('extendify_draft_settings', null),
[98] Fix | Delete
'activity' => \get_option('extendify_shared_activity', null),
[99] Fix | Delete
'domainsActivities' => \get_option('extendify_domains_recommendations_activities', null),
[100] Fix | Delete
'extendifyVersion' => ($version[1] ?? null),
[101] Fix | Delete
'siteProfile' => \get_option('extendify_site_profile', null),
[102] Fix | Delete
'pluginSearchTerms' => \get_option('extendify_plugin_search_terms', []),
[103] Fix | Delete
'blockSearchTerms' => \get_option('extendify_block_search_terms', []),
[104] Fix | Delete
'phpVersion' => PHP_VERSION,
[105] Fix | Delete
'themeSearchTerms' => \get_option('extendify_theme_search_terms', []),
[106] Fix | Delete
'license' => PartnerData::setting('license'),
[107] Fix | Delete
'pagesCount' => $this->getPostsCount('page'),
[108] Fix | Delete
'postsCount' => $this->getPostsCount('post'),
[109] Fix | Delete
'lastUpdatedPage' => $this->getLastUpdatedPost('page'),
[110] Fix | Delete
'lastUpdatedPost' => $this->getLastUpdatedPost('post'),
[111] Fix | Delete
'lastLoginAdmin' => $this->getLastAdminLogin(),
[112] Fix | Delete
'hasImprint' => $this->hasImprint(),
[113] Fix | Delete
]);
[114] Fix | Delete
return $insights;
[115] Fix | Delete
});
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Get the number of posts/pages
[120] Fix | Delete
*
[121] Fix | Delete
* @param string $type The type of post/page to get the count for (post or page)
[122] Fix | Delete
* @return int The number of posts/pages
[123] Fix | Delete
*/
[124] Fix | Delete
protected function getPostsCount($type = 'post')
[125] Fix | Delete
{
[126] Fix | Delete
$count = wp_count_posts($type);
[127] Fix | Delete
return isset($count->publish) ? (int) $count->publish : 0;
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
/**
[131] Fix | Delete
* Set up admin login insights to monitor when admin users log in
[132] Fix | Delete
*
[133] Fix | Delete
* @return void
[134] Fix | Delete
*/
[135] Fix | Delete
protected function setupAdminLoginInsights()
[136] Fix | Delete
{
[137] Fix | Delete
add_action('wp_login', function ($user_login, $user) {
[138] Fix | Delete
// Only get insights for admin users
[139] Fix | Delete
if (user_can($user, 'manage_options')) {
[140] Fix | Delete
update_user_meta($user->ID, 'extendify_last_login', gmdate('Y-m-d H:i:s'));
[141] Fix | Delete
}
[142] Fix | Delete
}, 10, 2);
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Get the last time a post/page was updated
[147] Fix | Delete
*
[148] Fix | Delete
* @return string|null The last updated post timestamp or null if no posts found
[149] Fix | Delete
*/
[150] Fix | Delete
protected function getLastUpdatedPost($type = 'post')
[151] Fix | Delete
{
[152] Fix | Delete
$posts = get_posts([
[153] Fix | Delete
'post_type' => $type,
[154] Fix | Delete
'post_status' => 'publish',
[155] Fix | Delete
'orderby' => 'modified',
[156] Fix | Delete
'order' => 'DESC',
[157] Fix | Delete
'numberposts' => 1,
[158] Fix | Delete
'fields' => 'ids'
[159] Fix | Delete
]);
[160] Fix | Delete
[161] Fix | Delete
if (!empty($posts)) {
[162] Fix | Delete
$post = get_post($posts[0]);
[163] Fix | Delete
return $post ? $post->post_modified : null;
[164] Fix | Delete
}
[165] Fix | Delete
return null;
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
/**
[169] Fix | Delete
* Get the last time an admin user logged in
[170] Fix | Delete
*
[171] Fix | Delete
* @return string|null The most recent admin login timestamp or null if no data found
[172] Fix | Delete
*/
[173] Fix | Delete
protected function getLastAdminLogin()
[174] Fix | Delete
{
[175] Fix | Delete
$admins = get_users([
[176] Fix | Delete
'role' => 'administrator',
[177] Fix | Delete
'meta_key' => 'extendify_last_login',
[178] Fix | Delete
'orderby' => 'meta_value',
[179] Fix | Delete
'order' => 'DESC',
[180] Fix | Delete
'number' => 1
[181] Fix | Delete
]);
[182] Fix | Delete
[183] Fix | Delete
if (!empty($admins)) {
[184] Fix | Delete
return get_user_meta($admins[0]->ID, 'extendify_last_login', true);
[185] Fix | Delete
}
[186] Fix | Delete
return null;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Check if the site has an imprint based on the site profile and language settings
[191] Fix | Delete
*
[192] Fix | Delete
* @return bool True if the site has an imprint, false otherwise
[193] Fix | Delete
*/
[194] Fix | Delete
protected function hasImprint()
[195] Fix | Delete
{
[196] Fix | Delete
$siteProfile = \get_option('extendify_site_profile', []);
[197] Fix | Delete
if (empty($siteProfile)) {
[198] Fix | Delete
return false;
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
$imprintLanguages = array_filter(PartnerData::setting('showImprint') ?? [], function ($value) {
[202] Fix | Delete
return $value === get_locale();
[203] Fix | Delete
});
[204] Fix | Delete
[205] Fix | Delete
return !empty($imprintLanguages) && (strtolower($siteProfile['aiSiteCategory']) === 'business' ||
[206] Fix | Delete
strtolower($siteProfile['category']) === 'business');
[207] Fix | Delete
}
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function