PHP code example of stellarwp / telemetry

1. Go to this page and download the library: Download stellarwp/telemetry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stellarwp / telemetry example snippets


use StellarWP\Telemetry\Core as Telemetry;

add_action( 'plugins_loaded', 'initialize_telemetry' );

function initialize_telemetry() {
	/**
	 * Configure the container.
	 *
	 * The container must be compatible with stellarwp/container-contract.
	 * See here: https://github.com/stellarwp/container-contract#usage.
	 *
	 * If you do not have a container, we recommend https://github.com/lucatume/di52
	 * and the corresponding wrapper:
	 * https://github.com/stellarwp/container-contract/blob/main/examples/di52/Container.php
	 */
	$container = new Container();
	Config::set_container( $container );

	// Set the full URL for the Telemetry Server API.
	Config::set_server_url( 'https://telemetry.example.com/api/v1' );

	// Set a unique prefix for actions & filters.
	Config::set_hook_prefix( 'my-custom-prefix' );

	// Set a unique plugin slug.
	Config::set_stellar_slug( 'my-custom-stellar-slug' );

    // Initialize the library.
    Telemetry::instance()->init( __FILE__ );
}

add_action( 'plugins_loaded', 'hook_into_existing_telemetry' );

function hook_into_existing_telemetry() {
	// Check to make sure that the Telemetry library is already instantiated.
	if ( ! class_exists( Telemetry::class ) ) {
		return;
	}

	// Register the current plugin with an already instantiated library.
	Config::add_stellar_slug( 'my-custom-stellar-slug', 'custom-plugin/custom-plugin.php' );
}

// uninstall.php

use YOUR_STRAUSS_PREFIX\StellarWP\Telemetry\Uninstall;


do_action( 'stellarwp/telemetry/optin', '{stellar_slug}' );

/**
 * Planned Deprecation: 3.0.0
 *
 * Please use 'stellarwp/telemetry/optin' action instead.
 */
do_action( 'stellarwp/telemetry/{stellar_slug}/optin' );

function my_options_page() {
    do_action( 'stellarwp/telemetry/optin', '{stellar_slug}' );
    

do_action( 'stellarwp/telemetry/optin', '{stellar_slug}', [ 'plugin_slug' => 'the-events-calendar' ] );

add_action( 'admin_init', 'save_opt_in_setting_field' );

/**
 * Saves the "Opt In Status" setting.
 *
 * @return void
 */
public function save_opt_in_setting_field() {
	// Return early if not saving the Opt In Status field.
	if ( ! isset( $_POST[ 'opt-in-status' ] ) ) {
		return;
	}

	// Get an instance of the Status class.
	$Status = Config::get_container()->get( Status::class );

	// Get the value submitted on the settings page as a boolean.
	$value = filter_input( INPUT_POST, 'opt-in-status', FILTER_VALIDATE_BOOL );

	$Status->set_status( $value );
}

/**
 * The library attempts to set the opt-in status for a site during 'admin_init'. Use the hook with a priority higher
 * than 10 to make sure you're setting the status after it initializes the option in the options table.
 */
add_action( 'admin_init', 'migrate_existing_opt_in', 11 );

function migrate_existing_opt_in() {

	if ( $user_has_opted_in_already ) {

		// Get the Opt_In_Subscriber object.
		$Opt_In_Subscriber = Config::get_container()->get( Opt_In_Subscriber::class );
		$Opt_In_Subscriber->opt_in();
	}
}

use STRAUSS_PREFIX\StellarWP\Telemetry\Config;

add_action( 'plugins_loaded', 'add_plugin_to_telemetry' );

function add_plugin_to_telemetry() {

	// Verify that Telemetry is available.
	if ( ! class_exists( Config::class ) ) {
        return;
    }

	// Set a unique plugin slug and 

// Event data is sent to the telemetry server as JSON.
$data = [
	'one'   => 1,
	'two'   => 2,
	'three' => 3,
];
do_action( 'stellarwp/telemetry/{hook-prefix}/event', 'your-event-name', $data );

/**
 * Log event when a user creates a new post.
 *
 * @action save_post
 *
 * @param int     $post_id The ID of the post being saved.
 * @param WP_Post $post    The post object being saved.
 * @param bool    $update  If this is an update to a pre-existing post.
 *
 * @return void
 */
function user_creates_post( $post_id, $post, $update ) {
	// Only send events for new posts.
	if ( $update ) {
		return;
	}

	// Only send event for posts, avoid everything else.
	if ( $post->post_type !== 'post' ) {
		return;
	}

	// Add any data to the event that needs to be captured.
	$event_data = [
		'title'   => $post->post_title,
		'content' => $post->post_content,
		'some-other-data' => 'use the array to capture anything else that might be necessary for context'
	];

	// Log the event with the telemetry server.
	do_action( 'stellarwp/telemetry/{hook-prefix}/event', 'new_post', $event_data );
}