11<?php
22/*
33Plugin Name: Broadly for WordPress
4- Description: Dynamic integration of your Broadly reviews within your existing WordPress website.
4+ Description: Dynamic integration of your Broadly reviews within your existing WordPress website.
55Plugin URL: http://broadly.com
66Author: Broadly
77Author URI: http://broadly.com/
8- Version: 2.0
8+ Version: 2.0.1
99License: GPLv2 or later
1010*/
1111
1212if ( ! class_exists ( 'Broadly_Plugin ' ) ) {
13-
13+
1414 /**
1515 * Main Broadly Plugin class
16- *
17- * Responsible for the frontend review management and backend
16+ *
17+ * Responsible for the frontend review management and backend
1818 * settings for the plugin.
19- *
19+ *
2020 * @author nofearinc
2121 *
2222 */
2323 class Broadly_Plugin {
24-
24+
25+ public static $ version = '2.0.1 ' ;
26+
2527 function __construct () {
2628 // Creating the admin menu
2729 add_action ( 'admin_menu ' , array ( $ this , 'broadly_menu ' ) );
28-
30+
2931 // Register the Settings fields
3032 add_action ( 'admin_init ' , array ( $ this , 'broadly_settings_init ' ) );
31-
33+
3234 // Replace the Broadly scripts with the prefetched HTML
3335 add_filter ( 'the_content ' , array ( $ this , 'replace_js ' ) );
3436 }
@@ -37,30 +39,30 @@ function __construct() {
3739 * Register a menu page for Broadly under Settings
3840 */
3941 public function broadly_menu () {
40- add_options_page ( __ ('Broadly ' , 'broadly ' ),
42+ add_options_page ( __ ('Broadly ' , 'broadly ' ),
4143 __ ( 'Broadly Setup ' , 'broadly ' ),
4244 'manage_options ' , 'broadly ' , array ( $ this , 'broadly_menu_cb ' ) );
4345 }
44-
46+
4547 /**
4648 * Settings class initialization
4749 */
4850 public function broadly_settings_init () {
4951 include_once 'settings.class.php ' ;
5052 }
51-
53+
5254 /**
5355 * Menu page callback - render the UI form for the admin
5456 */
5557 public function broadly_menu_cb () {
5658 $ broadly_options = get_option ( 'broadly_options ' , array () );
57-
59+
5860 include_once 'settings-page.php ' ;
5961 }
6062
6163 /**
6264 * Replace the JS snippet with the prefetched reviews
63- *
65+ *
6466 * @param string $content the existing page content
6567 * @return string $content the updated page content if a script is found
6668 */
@@ -70,53 +72,103 @@ public function replace_js( $content ) {
7072
7173 // Proceed further only if a match is found - false will handle both 0 and false
7274 if ( false != $ matches_count ) {
73-
75+
7476 // Iterate through all of the matches if more scripts are injected
7577 for ( $ current_match = 0 ; $ current_match < $ matches_count ; $ current_match ++ ) {
76-
78+
7779 // Fetch the entire script and the data-url match
7880 $ script_match = $ matches [0 ][$ current_match ];
7981 $ dataurl_match = $ matches [1 ][$ current_match ];
80-
81- // Append the data-url and build the reviews URL
82- $ broadly_reviews_url = 'http://embed.broadly.com/ ' . $ dataurl_match ;
83-
82+
83+ // Append the data-url and build the embed URL
84+ $ broadly_embed_url = 'http://embed.broadly.com/ ' . $ dataurl_match ;
85+
8486 $ args = array ();
8587 /**
8688 * Hook the arguments for the remote call.
87- *
89+ *
8890 * If needed, we can disable SSL or update the other HTTP arguments.
8991 */
9092 $ args = apply_filters ( 'broadly_ssl_args ' , $ args );
91-
92- $ response = wp_remote_get ( $ broadly_reviews_url , $ args );
93-
93+
94+ add_filter ( 'http_request_args ' , array ( $ this , 'filter_broadly_headers ' ), 10 , 2 );
95+ $ response = wp_remote_get ( $ broadly_embed_url , $ args );
96+ remove_filter ( 'http_request_args ' , array ( $ this , 'filter_broadly_headers ' ), 10 );
97+
9498 // Verify for errors - not being sent for reporting yet
9599 $ error = null ;
96100 if ( is_wp_error ( $ response ) ) {
97101 $ error = __ ('Error Found ( ' . $ response ->get_error_message () . ' ) ' , 'broadly ' );
98102 } else {
99103 if ( ! empty ( $ response ["body " ] ) ) {
100- $ review = $ response ["body " ];
104+ $ embed_content = $ response ["body " ];
101105 } else {
102106 $ error = __ ( 'No body tag in the response ' , 'broadly ' );
103107 }
104108 }
105-
109+
106110 // If errors occured, don't replace the script tags
107111 if ( ! is_null ( $ error ) ) {
108112 continue ;
109113 }
110114
115+ // Wrapper tabs
116+ $ embed_name = $ this ->parse_embed_from_datauri ( $ dataurl_match );
117+ $ opening_comment = sprintf ( '<!-- Start of Broadly "%s" content - Broadly for WordPress %s --> ' , $ embed_name , self ::$ version );
118+ $ closing_comment = sprintf ( '<!-- End of Broadly "%s" content --> ' , $ embed_name );
119+
111120 // Replace the script tag with the HTML reviews
112- $ content = str_replace ( $ script_match , $ review , $ content );
121+ $ content = str_replace ( $ script_match , $ opening_comment . $ embed_content . $ closing_comment , $ content );
113122 }
114123 }
115-
124+
116125 return $ content ;
117126 }
127+
128+ /**
129+ * Filter Broadly Headers
130+ *
131+ * @param $request_args original request arguments
132+ * @param $url request URL
133+ */
134+ public function filter_broadly_headers ( $ request_args , $ url ) {
135+ $ user_agent = sprintf ( '%s; Broadly/%s ' , $ request_args ['user-agent ' ], self ::$ version );
136+
137+ // Update the User-Agent header
138+ $ headers = $ request_args ['headers ' ];
139+ $ headers ['User-Agent ' ] = $ user_agent ;
140+
141+ // Set the Referer header if possible
142+ $ current_page = get_the_permalink ();
143+ if ( false !== $ current_page ) {
144+ $ headers ['Referer ' ] = $ current_page ;
145+ }
146+
147+ // Update the original headers
148+ $ request_args ['headers ' ] = $ headers ;
149+
150+ return $ request_args ;
151+ }
152+
153+ /**
154+ * Parse the type of embed from the data_uri
155+ *
156+ * @param $data_uri argument passed to Broadly including the embed name
157+ */
158+ private function parse_embed_from_datauri ( $ data_uri ) {
159+ // Split the data uri in attempt to read the type
160+ $ components = explode ( '/ ' , $ data_uri );
161+
162+ $ embed_name = 'reviews ' ;
163+
164+ if ( isset ( $ components [1 ] ) ) {
165+ $ embed_name = $ components [1 ];
166+ }
167+
168+ return $ embed_name ;
169+ }
118170 }
119-
171+
120172 // Initialize the plugin body
121173 $ broadly = new Broadly_Plugin ();
122174}
0 commit comments