Skip to content

Instantly share code, notes, and snippets.

@Zodiac1978
Created March 22, 2019 10:08
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 Zodiac1978/a4796579f5143d9e3100015e664adc2c to your computer and use it in GitHub Desktop.
Save Zodiac1978/a4796579f5143d9e3100015e664adc2c to your computer and use it in GitHub Desktop.
Adds a new column in the statify table for mobile usage.
<?php
/**
* Plugin Name: Statify Mobile Tracking
* Description: Adds a new column in the statify table for mobile usage.
* Plugin URI: http://torstenlandsiedel.de
* Version: 1.0
* Author: Torsten Landsiedel
* Author URI: http://torstenlandsiedel.de
* Licence: GPL 2
* License URI: http://opensource.org/licenses/GPL-2.0
*
* @package Statify/Mobile Tracking
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Add new column ismobile in statify table on plugin activation
*
* @since 1.0
*/
function statify_mobile_tracking_activate() {
global $wpdb;
if ( $wpdb->get_var( "SHOW columns from `$wpdb->statify` WHERE field='ismobile'" ) ) {
return;
}
// Is the column already there?
//$row = $wpdb->get_results( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$wpdb->statify' AND column_name = 'useragent'" );
//$row = $wpdb->get_results( "SHOW columns from '$wpdb->statify' WHERE field='useragent'" );
// If not, create the column.
//if ( empty( $row ) ) {
$wpdb->query( "ALTER TABLE `$wpdb->statify` ADD ismobile bool;" );
//}
}
register_activation_hook( __FILE__, 'statify_mobile_tracking_activate' );
/**
* Add if view is mobile or not on statify__visit_saved action hook.
*
* @since 1.0
*
* @param array $data Array of data for visit.
* @param string $id ID of the data set.
*/
function statify_mobile_tracking_save_visit( $data, $id ) {
global $wpdb;
$result = $wpdb->update(
$wpdb->statify, // Which table is updated?
array(
'ismobile' => wp_is_mobile(), // Which data is changed/added?
),
array( 'ID' => $id ), // Where is the data changed?
array(
'%d', // Format for added value (because 0/1 in MySQL).
),
array( '%d' ) // Format for where data id.
);
if (false === $result) {
// Nichts aktualisiert.
}
}
add_action( 'statify__visit_saved', 'statify_mobile_tracking_save_visit', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment