Advertisement
Guest User

Yoast SEO

a guest
May 13th, 2014
203
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.06 KB | None
  1. <?php
  2. /**
  3.  * @package Admin
  4.  */
  5.  
  6. if ( ! defined( 'WPSEO_VERSION' ) ) {
  7.     header( 'Status: 403 Forbidden' );
  8.     header( 'HTTP/1.1 403 Forbidden' );
  9.     exit();
  10. }
  11.  
  12. if ( ! class_exists( 'Yoast_Tracking' ) ) {
  13.     /**
  14.      * Class that creates the tracking functionality for WP SEO, as the core class might be used in more plugins,
  15.      * it's checked for existence first.
  16.      *
  17.      * NOTE: this functionality is opt-in. Disabling the tracking in the settings or saying no when asked will cause
  18.      * this file to not even be loaded.
  19.      *
  20.      * @todo [JRF => testers] check if tracking still works if an old version of the Yoast Tracking class was loaded
  21.      * (i.e. another plugin loaded their version first)
  22.      */
  23.     class Yoast_Tracking {
  24.  
  25.         /**
  26.          * @var object  Instance of this class
  27.          */
  28.         public static $instance;
  29.  
  30.  
  31.         /**
  32.          * Class constructor
  33.          */
  34.         function __construct() {
  35.             // Constructor is called from WP SEO
  36.             if ( current_filter( 'yoast_tracking' ) ) {
  37.                 $this->tracking();
  38.             }
  39.             // Backward compatibility - constructor is called from other Yoast plugin
  40.             elseif ( ! has_action( 'yoast_tracking', array( $this, 'tracking' ) ) ) {
  41.                 add_action( 'yoast_tracking', array( $this, 'tracking' ) );
  42.             }
  43.         }
  44.  
  45.         /**
  46.          * Get the singleton instance of this class
  47.          *
  48.          * @return object
  49.          */
  50.         public static function get_instance() {
  51.             if ( ! ( self::$instance instanceof self ) ) {
  52.                 self::$instance = new self();
  53.             }
  54.             return self::$instance;
  55.         }
  56.  
  57.         /**
  58.          * Main tracking function.
  59.          */
  60.         function tracking() {
  61.  
  62.             $data = get_transient( 'yoast_tracking_cache' );
  63.  
  64.             // bail if transient is set and valid
  65.             if( $data !== false ) {
  66.                 return;
  67.             }
  68.  
  69.             // Start of Metrics
  70.             global $blog_id, $wpdb;
  71.  
  72.             $hash = get_option( 'Yoast_Tracking_Hash', false );
  73.  
  74.             if ( ! $hash || empty( $hash ) ) {
  75.                 // create and store hash
  76.                 $hash = md5( site_url() );
  77.                 update_option( 'Yoast_Tracking_Hash', $hash );
  78.             }
  79.  
  80.             $pts        = array();
  81.             $post_types = get_post_types( array( 'public' => true ) );
  82.             if ( is_array( $post_types ) && $post_types !== array() ) {
  83.                 foreach ( $post_types as $post_type ) {
  84.                     $count           = wp_count_posts( $post_type );
  85.                     $pts[$post_type] = $count->publish;
  86.                 }
  87.             }
  88.             unset( $post_types );
  89.  
  90.             $comments_count = wp_count_comments();
  91.  
  92.             $theme_data = wp_get_theme();
  93.             $theme      = array(
  94.                 'name'       => $theme_data->display( 'Name', false, false ),
  95.                 'theme_uri'  => $theme_data->display( 'ThemeURI', false, false ),
  96.                 'version'    => $theme_data->display( 'Version', false, false ),
  97.                 'author'     => $theme_data->display( 'Author', false, false ),
  98.                 'author_uri' => $theme_data->display( 'AuthorURI', false, false ),
  99.             );
  100.             $theme_template = $theme_data->get_template();
  101.             if ( $theme_template !== '' && $theme_data->parent() ) {
  102.                 $theme['template'] = array(
  103.                     'version'    => $theme_data->parent()->display( 'Version', false, false ),
  104.                     'name'       => $theme_data->parent()->display( 'Name', false, false ),
  105.                     'theme_uri'  => $theme_data->parent()->display( 'ThemeURI', false, false ),
  106.                     'author'     => $theme_data->parent()->display( 'Author', false, false ),
  107.                     'author_uri' => $theme_data->parent()->display( 'AuthorURI', false, false ),
  108.                 );
  109.             }
  110.             else {
  111.                 $theme['template'] = '';
  112.             }
  113.             unset( $theme_template );
  114.  
  115.  
  116.             $plugins = array();
  117.             $active_plugin = get_option( 'active_plugins' );
  118.             foreach ( $active_plugin as $plugin_path ) {
  119.                 if ( ! function_exists( 'get_plugin_data' ) ) {
  120.                     require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  121.                 }
  122.  
  123.                 $plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_path );
  124.  
  125.                 $slug           = str_replace( '/' . basename( $plugin_path ), '', $plugin_path );
  126.                 $plugins[$slug] = array(
  127.                     'version'    => $plugin_info['Version'],
  128.                     'name'       => $plugin_info['Name'],
  129.                     'plugin_uri' => $plugin_info['PluginURI'],
  130.                     'author'     => $plugin_info['AuthorName'],
  131.                     'author_uri' => $plugin_info['AuthorURI'],
  132.                 );
  133.             }
  134.             unset( $active_plugins, $plugin_path );
  135.  
  136.             $data = array(
  137.                 'site'     => array(
  138.                     'hash'      => $hash,
  139.                     'version'   => get_bloginfo( 'version' ),
  140.                     'multisite' => is_multisite(),
  141.                     'users'     => $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->users INNER JOIN $wpdb->usermeta ON ({$wpdb->users}.ID = {$wpdb->usermeta}.user_id) WHERE 1 = 1 AND ( {$wpdb->usermeta}.meta_key = %s )", 'wp_' . $blog_id . '_capabilities' ) ),
  142.                     'lang'      => get_locale(),
  143.                 ),
  144.                 'pts'      => $pts,
  145.                 'comments' => array(
  146.                     'total'    => $comments_count->total_comments,
  147.                     'approved' => $comments_count->approved,
  148.                     'spam'     => $comments_count->spam,
  149.                     'pings'    => $wpdb->get_var( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_type = 'pingback'" ),
  150.                 ),
  151.                 'options'  => apply_filters( 'yoast_tracking_filters', array() ),
  152.                 'theme'    => $theme,
  153.                 'plugins'  => $plugins,
  154.             );
  155.  
  156.             $args = array(
  157.                 'body' => $data,
  158.             );
  159.             wp_remote_post( 'https://tracking.yoast.com/', $args );
  160.  
  161.             // Store for a week, then push data again.
  162.             set_transient( 'yoast_tracking_cache', true, 60 * 60 * 24 * 7 );
  163.  
  164.         }
  165.     } /* End of class */
  166. } /* End of class-exists wrapper */
  167.  
  168. /**
  169.  * Adds tracking parameters for WP SEO settings. Outside of the main class as the class could also be in use in other plugins.
  170.  *
  171.  * @param array $options
  172.  * @return array
  173.  */
  174. function wpseo_tracking_additions( $options ) {
  175.     $opt = WPSEO_Options::get_all();
  176.  
  177.     $options['wpseo'] = array(
  178.         'xml_sitemaps'        => ( $opt['enablexmlsitemap'] === true ) ? 1 : 0,
  179.         'force_rewrite'       => ( $opt['forcerewritetitle'] === true ) ? 1 : 0,
  180.         'opengraph'           => ( $opt['opengraph'] === true ) ? 1 : 0,
  181.         'twitter'             => ( $opt['twitter'] === true ) ? 1 : 0,
  182.         'strip_category_base' => ( $opt['stripcategorybase'] === true ) ? 1 : 0,
  183.         'on_front'            => get_option( 'show_on_front' ),
  184.     );
  185.     return $options;
  186. }
  187.  
  188. add_filter( 'yoast_tracking_filters', 'wpseo_tracking_additions' );
Advertisement
RAW Paste Data Copied
Advertisement