Skip to content

Commit

Permalink
Merge pull request #345 from Parsely/experimental/refactor-integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryJones authored Sep 21, 2021
2 parents 7494edb + 887e3c5 commit 4128196
Show file tree
Hide file tree
Showing 11 changed files with 624 additions and 206 deletions.
129 changes: 129 additions & 0 deletions src/Integrations/class-amp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* AMP integration class
*
* @package Parsely\Integrations
* @since 2.6.0
*/

namespace Parsely\Integrations;

/**
* Integrates Parse.ly tracking with the AMP plugin.
*
* @since 2.6.0 Moved from Parsely class to this file.
*/
class Amp implements Integration {
/**
* Apply the hooks that integrate the plugin or theme with the Parse.ly plugin.
*
* @since 2.6.0
*/
public function integrate() {
if ( defined( 'AMP__VERSION' ) ) {
add_action( 'template_redirect', array( $this, 'add_actions' ) );
}
}

/**
* Verify if request is an AMP request.
*
* This is needed to make it easier to mock whether the function exists ot not during tests.
*
* @codeCoverageIgnore
*
* @since 2.6.0
*
* @return bool True is an AMP request, false otherwise.
*/
public function is_amp_request() {
return function_exists( 'amp_is_request' ) && amp_is_request();
}

/**
* Verify if request is an AMP request, and that AMP support is not disabled.
*
* @since 2.6.0
*
* @return bool True is an AMP request and not disabled, false otherwise.
*/
public function can_handle_amp_request() {
$options = get_option( \Parsely::OPTIONS_KEY );

return $this->is_amp_request() && is_array( $options ) && ! $options['disable_amp'];
}

/**
* Add AMP actions.
*
* @since 2.6.0
*/
public function add_actions() {
if ( $this->can_handle_amp_request() ) {
add_filter( 'amp_post_template_analytics', array( $this, 'register_parsely_for_amp_analytics' ) );
add_filter( 'amp_analytics_entries', array( $this, 'register_parsely_for_amp_native_analytics' ) );
}
}

/**
* Register Parse.ly for AMP analytics.
*
* @since 2.6.0
*
* @param array $analytics The analytics registry.
* @return array The analytics registry.
*/
public function register_parsely_for_amp_analytics( $analytics ) {
$options = get_option( \Parsely::OPTIONS_KEY );

if ( empty( $options['apikey'] ) ) {
return $analytics;
}

$analytics['parsely'] = array(
'type' => 'parsely',
'attributes' => array(),
'config_data' => array(
'vars' => array(
'apikey' => $options['apikey'],
),
),
);

return $analytics;
}

/**
* Register Parse.ly for AMP native analytics.
*
* @since 2.6.0
*
* @param array $analytics The analytics registry.
* @return array The analytics registry.
*/
public function register_parsely_for_amp_native_analytics( $analytics ) {
$options = get_option( \Parsely::OPTIONS_KEY );

if ( ! empty( $options['disable_amp'] ) && true === $options['disable_amp'] ) {
return $analytics;
}

if ( empty( $options['apikey'] ) ) {
return $analytics;
}

$analytics['parsely'] = array(
'type' => 'parsely',
'attributes' => array(),
'config' => wp_json_encode(
array(
'vars' => array(
'apikey' => $options['apikey'],
),
)
),
);

return $analytics;
}
}
73 changes: 73 additions & 0 deletions src/Integrations/class-facebook-instant-articles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Facebook Instant Articles integration class
*
* @package Parsely\Integrations
* @since 2.6.0
*/

namespace Parsely\Integrations;

/**
* Integrates Parse.ly tracking with the Facebook Instant Articles plugin.
*
* @since 2.6.0 Moved from Parsely class to this file.
*/
final class Facebook_Instant_Articles implements Integration {
const REGISTRY_IDENTIFIER = 'parsely-analytics-for-wordpress';
const REGISTRY_DISPLAY_NAME = 'Parse.ly Analytics';

/**
* Apply the hooks that integrate the plugin or theme with the Parse.ly plugin.
*
* @since 2.6.0
*/
public function integrate() {
if ( defined( 'IA_PLUGIN_VERSION' ) ) {
add_action( 'instant_articles_compat_registry_analytics', array( $this, 'insert_parsely_tracking' ) );
}
}

/**
* Add Parse.ly tracking to Facebook instant articles.
*
* @since 2.6.0
*
* @param array $registry The registry info for fbia.
* @return void
*/
public function insert_parsely_tracking( &$registry ) {
$options = get_option( \Parsely::OPTIONS_KEY );
if ( ! ( $options['apikey'] ) ) {
return;
}

$registry[ self::REGISTRY_IDENTIFIER ] = array(
'name' => self::REGISTRY_DISPLAY_NAME,
'payload' => $this->get_embed_code( $options['apikey'] ),
);
}

/**
* Get the payload / embed code.
*
* @since 2.6.0
*
* @param string $api_key API key.
* @return string Embedded code.
*/
public function get_embed_code( $api_key ) {
return '<script>
PARSELY = {
autotrack: false,
onload: function() {
PARSELY.beacon.trackPageView({
urlref: \'http://facebook.com/instantarticles\'
});
return true;
}
}
</script>
<script data-cfasync="false" id="parsely-cfg" data-parsely-site="' . esc_attr( $api_key ) . '" src="//cdn.parsely.com/keys/' . esc_attr( $api_key ) . '/p.js"></script>';
}
}
23 changes: 23 additions & 0 deletions src/Integrations/class-integration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Integration interface
*
* @package Parsely\Integrations
* @since 2.6.0
*/

namespace Parsely\Integrations;

/**
* Integration classes are expected to implement this interface.
*
* @since 2.6.0
*/
interface Integration {
/**
* Apply the hooks that integrate the plugin or theme with the Parse.ly plugin.
*
* @since 2.6.0
*/
public function integrate();
}
53 changes: 53 additions & 0 deletions src/Integrations/class-integrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Integrations collection
*
* @package Parsely\Integrations
* @since 2.6.0
*/

namespace Parsely\Integrations;

/**
* Integrations are registered to this collection.
*
* The integrate() method is called on each registered integration, on the init hook.
*
* @since 2.6.0
*/
class Integrations {
/**
* Collection of registered integrations.
*
* @var array
*/
private $integrations = array();

/**
* Register an integration.
*
* @since 2.6.0
*
* @param string $key A unique identifier for the integration.
* @param string|object $class_or_object Fully-qualified class name, or an instantiated object.
* If a class name is passed, it will be instantiated.
*/
public function register( $key, $class_or_object ) {
// If a Foo::class or other FQCN is passed, instantiate it.
if ( ! is_object( $class_or_object ) ) {
$class_or_object = new $class_or_object();
}
$this->integrations[ $key ] = $class_or_object;
}

/**
* Integrate each integration by calling the method that does the add_action() and add_filter() calls.
*
* @since 2.6.0
*/
public function integrate() {
foreach ( $this->integrations as $integration ) {
$integration->integrate();
}
}
}
Loading

0 comments on commit 4128196

Please sign in to comment.