PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.75
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.75
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / public / class-pagehooks.php

class-pagehooks.php in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.3.75, at public/class-pagehooks.php

154 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Leadin;
4
5 use Leadin\data\Filters;
6 use Leadin\AssetsManager;
7 use Leadin\data\User;
8 use Leadin\auth\OAuth;
9 use Leadin\admin\Connection;
10 use Leadin\data\Portal_Options;
11 use Leadin\utils\ShortcodeRenderUtils;
12
13 /**
14 * Class responsible of adding the script loader to the website, as well as rendering forms, live chat, etc.
15 */
16 class PageHooks {
17 /**
18 * Class constructor, adds the necessary hooks.
19 */
20 public function __construct() {
21 add_action( 'init', array( $this, 'register_content_type_meta' ) );
22
23 add_action( 'wp_head', array( $this, 'add_page_analytics' ) );
24 if ( Connection::is_connected() ) {
25 add_action( 'wp_enqueue_scripts', array( $this, 'add_frontend_scripts' ) );
26 }
27 add_filter( 'script_loader_tag', array( $this, 'add_id_to_tracking_code' ), 10, 2 );
28 add_filter( 'script_loader_tag', array( $this, 'add_defer_to_forms_script' ), 10, 2 );
29 add_shortcode( 'hubspot', array( $this, 'leadin_add_hubspot_shortcode' ) );
30 }
31
32 /**
33 * Register meta key for content type
34 */
35 public function register_content_type_meta() {
36 register_post_meta(
37 '',
38 'content-type', // meta key.
39 array(
40 'object_subtype' => 'post', // specify a post type here.
41 'type' => 'string',
42 'single' => true,
43 'show_in_rest' => true,
44 'auth_callback' => function () {
45 return current_user_can( 'edit_posts' );
46 },
47 )
48 );
49 }
50
51 /**
52 * Adds the script loader to the page.
53 */
54 public function add_frontend_scripts() {
55 if ( Portal_Options::get_disable_internal_tracking() && ( is_user_logged_in() || current_user_can( 'install_plugins' ) ) ) {
56 return;
57 }
58
59 if ( is_single() ) {
60 $page_type = 'post';
61 } elseif ( is_front_page() ) {
62 $page_type = 'home';
63 } elseif ( is_archive() ) {
64 $page_type = 'archive';
65 } elseif ( is_page() ) {
66 $page_type = 'page';
67 } else {
68 $page_type = 'other';
69 }
70
71 $leadin_wordpress_info = array(
72 'userRole' => User::get_role(),
73 'pageType' => $page_type,
74 'leadinPluginVersion' => LEADIN_PLUGIN_VERSION,
75 );
76
77 AssetsManager::enqueue_script_loader( $leadin_wordpress_info );
78 }
79
80 /**
81 * Adds the script containing the information needed by the script loader.
82 */
83 public function add_page_analytics() {
84 $portal_id = Portal_Options::get_portal_id();
85
86 if ( empty( $portal_id ) ) {
87 echo '<!-- HubSpot WordPress Plugin v' . esc_html( LEADIN_PLUGIN_VERSION ) . ': embed JS disabled as a portalId has not yet been configured -->';
88 } else {
89 $content_type = Filters::apply_page_content_type_filters();
90 $page_id = get_the_ID();
91 $post_meta = get_post_meta( $page_id );
92 if ( isset( $post_meta['content-type'][0] ) && '' !== $post_meta['content-type'][0] ) {
93 $content_type = $post_meta['content-type'][0];
94 } elseif ( is_plugin_active( 'elementor/elementor.php' ) ) {
95 $page_settings_manager = \Elementor\Core\Settings\Manager::get_settings_managers( 'page' );
96 $page_settings_model = $page_settings_manager->get_model( $page_id );
97 $elementor_content_type = $page_settings_model->get_settings( 'content_type' );
98 if ( ! empty( $elementor_content_type ) ) {
99 $content_type = $elementor_content_type;
100 }
101 }
102
103 ?>
104 <!-- DO NOT COPY THIS SNIPPET! Start of Page Analytics Tracking for HubSpot WordPress plugin v<?php echo esc_html( LEADIN_PLUGIN_VERSION ); ?>-->
105 <script class="hsq-set-content-id" data-content-id="<?php echo esc_html( $content_type ); ?>">
106 var _hsq = _hsq || [];
107 _hsq.push(["setContentType", "<?php echo esc_html( $content_type ); ?>"]);
108 </script>
109 <!-- DO NOT COPY THIS SNIPPET! End of Page Analytics Tracking for HubSpot WordPress plugin -->
110 <?php
111 }
112 }
113
114 /**
115 * Add the required id to the script loader <script>
116 *
117 * @param String $tag tag name.
118 * @param String $handle handle.
119 */
120 public function add_id_to_tracking_code( $tag, $handle ) {
121 if ( AssetsManager::TRACKING_CODE === $handle ) {
122 $tag = preg_replace(
123 '/id=[\'"]' . preg_quote( $handle . '-js', '/' ) . '[\'"]/',
124 "async defer id='hs-script-loader'",
125 $tag
126 );
127 }
128 return $tag;
129 }
130
131 /**
132 * Add defer to leadin forms plugin
133 *
134 * @param String $tag tag name.
135 * @param String $handle handle.
136 */
137 public function add_defer_to_forms_script( $tag, $handle ) {
138 if ( AssetsManager::FORMS_SCRIPT === $handle || AssetsManager::FORMS_V4_SCRIPT === $handle ) {
139 $tag = str_replace( 'src', 'defer src', $tag );
140 }
141 return $tag;
142 }
143
144 /**
145 * Parse leadin shortcodes
146 *
147 * @param array $attributes Shortcode attributes.
148 */
149 public function leadin_add_hubspot_shortcode( $attributes ) {
150 return ShortcodeRenderUtils::render_shortcode( $attributes );
151 }
152
153 }
154