Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Last active August 28, 2023 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verygoodplugins/87baffbd6eb754fb5fc20b75b4d42401 to your computer and use it in GitHub Desktop.
Save verygoodplugins/87baffbd6eb754fb5fc20b75b4d42401 to your computer and use it in GitHub Desktop.
Stores log of all sites that call home looking for updates
<?php
/*
Plugin Name: WPF All Sites
Plugin URI: verygoodplugins.com
Description: Stores log of all sites that call home looking for updates
Version: 1.0
Author: vergoodplugins
Author URI: verygoodplugins.com
License:
*/
function update_all_sites( $response, $args, $license_id ) {
$site = $_REQUEST;
$site['date'] = date( 'Y-m-d H:i:s' );
$site['status'] = $response['license'];
if ( ! empty( $_REQUEST['action'] ) ) {
$site['status'] = $_REQUEST['action'];
}
if ( empty( $site['integrations'] ) ) {
$site['integrations'] = array();
}
if ( false === strpos( $site['url'], 'wpfusiondemo' ) && false === strpos( $site['url'], 'launchflowsdemo' ) ) {
all_sites_add_site( $site );
}
return $response;
}
add_filter( 'edd_remote_license_check_response', 'update_all_sites', 10, 3 );
add_filter( 'edd_remote_license_activation_response', 'update_all_sites', 10, 3 );
// Add / update a site in the table
function all_sites_add_site( $site ) {
global $wpdb;
$insert = array(
'site_name' => urldecode( $site['url'] ),
'customer_id' => isset( $site['edd_item_id'] ) ? $site['edd_item_id'] : false,
'license_key' => isset( $site['license'] ) ? $site['license'] : false,
'status' => isset( $site['status'] ) ? $site['status'] : false,
'version' => isset( $site['version'] ) ? $site['version'] : false,
'check_date' => date( 'Y-m-d H:i:s', strtotime( $site['date'] ) ),
'item_name' => isset( $site['item_name'] ) ? urldecode( $site['item_name'] ) : false,
'crm' => isset( $site['crm'] ) ? $site['crm'] : false,
'integrations' => maybe_serialize( $site['integrations'] ),
);
$format = array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
);
// Check for existing
$result = $wpdb->get_row( $wpdb->prepare( "SELECT site_id FROM {$wpdb->prefix}wpf_all_sites WHERE site_name = %s", $insert['site_name'] ) );
if ( empty( $result ) ) {
$result = $wpdb->insert( "{$wpdb->prefix}wpf_all_sites", $insert, $format );
} else {
$result = $wpdb->update( "{$wpdb->prefix}wpf_all_sites", $insert, array( 'site_id' => $result->site_id ), $format );
}
return $result;
}
// Create the table if needed
function all_sites_create_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'wpf_all_sites';
if ( $wpdb->get_var( "show tables like '$table_name'" ) != $table_name ) {
$collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
$collate = $wpdb->get_charset_collate();
}
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = 'CREATE TABLE ' . $table_name . " (
site_id bigint(20) NOT NULL AUTO_INCREMENT,
customer_id varchar(20),
site_name varchar(128) NOT NULL,
license_key varchar(128),
status varchar(20),
version varchar(20),
check_date datetime NOT NULL,
item_name varchar(128),
crm varchar(128),
integrations longtext,
PRIMARY KEY (site_id)
) $collate;";
dbDelta( $sql );
}
}
//add_action( 'init', 'all_sites_create_table' );
function all_sites_count_sites_shortcode() {
$value = wp_cache_get( 'all_sites_count' );
if ( empty( $value ) ) {
global $wpdb;
$query_count = "SELECT COUNT(site_id) FROM {$wpdb->prefix}wpf_all_sites WHERE check_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)";
$value = $wpdb->get_var( $query_count );
wp_cache_set( 'all_sites_count', $value, '', HOUR_IN_SECONDS );
}
$value += 4000; // add in the wp.org sites.
return number_format( $value );
}
add_shortcode( 'all_sites_count_sites', 'all_sites_count_sites_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment