Changeset 2423044
- Timestamp:
- 11/22/2020 02:59:04 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
hyperise-opengraph-tags/trunk/hyperise-opengraph-tags.php
r2201299 r2423044 3 3 * Plugin Name: HYPERISE OpenGraph Tags for personalised link previews on LinkedIn 4 4 * Description: This plugin leverages Hyperise.com dynamic images to display personalised images in the preview pane, when you share a link with the utm_hyperef parameter added. Facebook OG tags into your blog's single posts which include Blog Title, Post Title, Description and Dynamic Image (if available). 5 * Version: 1.75 * Version: 2.0 6 6 * Author: HYPERISE 7 7 * Author URI: https://hyperise.com … … 20 20 remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); 21 21 22 add_filter( 'wpseo_title', 'yoast_add_keywords', 10, 1 ); 23 add_filter( 'wpseo_opengraph_image', 'yoast_add_image', 10, 1 ); 24 add_filter( 'wpseo_canonical', 'yoast_add_url', 10, 1 ); 25 add_filter( 'wpseo_opengraph_url', 'yoast_add_url', 10, 1 ); 26 27 function yoast_add_url( $str ) { 22 23 if (isset($_GET['utm_hyperef']) && strlen($_GET['utm_hyperef'])) { 24 //JetPack 25 add_filter( 'jetpack_enable_open_graph', '__return_false' ); 26 27 // yeost 28 add_filter( 'wpseo_title', 'add_keywords', 10, 1 ); 29 add_filter( 'wpseo_opengraph_image', 'add_image', 10, 1 ); 30 add_filter( 'wpseo_canonical', 'add_url', 10, 1 ); 31 add_filter( 'wpseo_opengraph_url', 'add_url', 10, 1 ); 32 33 // rankmath https://rankmath.com/kb/filters-hooks-api-developer/ 34 add_filter( 'rank_math/frontend/title', 'add_keywords', 10, 1 ); 35 add_filter( 'rank_math/opengraph/facebook/image', 'add_image', 10, 1); 36 add_filter( 'rank_math/opengraph/url', 'add_url', 10, 1); 37 add_filter( 'rank_math/frontend/title', 'add_keywords', 10 ,1); 38 } 39 40 function add_url( $str ) { 28 41 global $EnrichedData; 29 42 if($EnrichedData->utm_hyperef == '') { 30 43 $EnrichedData=hyperise_enrichdata(); 31 44 } 32 $EnrichedData-> yoastInstalled=true;45 $EnrichedData->seoInstalled=true; 33 46 return $str.'?utm_hyperef='.$EnrichedData->utm_hyperef; 34 47 } 35 function yoast_add_keywords( $str ) { 48 49 function add_keywords( $str ) { 36 50 global $EnrichedData; 37 51 if($EnrichedData->m_meta_title == '') { 38 52 $EnrichedData=hyperise_enrichdata(); 39 53 } 40 $EnrichedData-> yoastInstalled=true;54 $EnrichedData->seoInstalled=true; 41 55 return $EnrichedData->m_meta_title; 42 56 } 43 function yoast_add_image( $str ) {57 function add_image( $str ) { 44 58 global $EnrichedData; 45 59 if($EnrichedData->image_url == '') { 46 60 $EnrichedData=hyperise_enrichdata(); 47 61 } 48 $EnrichedData-> yoastInstalled=true;62 $EnrichedData->seoInstalled=true; 49 63 return $EnrichedData->image_url; 50 64 } 51 65 52 66 function hyperise_save_postdata($post_id){ 53 if (array_key_exists('hyperise_field ', $_POST)) {67 if (array_key_exists('hyperise_field_hash', $_POST)) { 54 68 update_post_meta( 55 69 $post_id, 56 '_hyperise_meta_ key',57 $_POST['hyperise_field ']70 '_hyperise_meta_hash', 71 $_POST['hyperise_field_hash'] 58 72 ); 59 73 } 74 if (array_key_exists('hyperise_field_title', $_POST)) { 75 update_post_meta( 76 $post_id, 77 '_hyperise_meta_title', 78 $_POST['hyperise_field_title'] 79 ); 80 } 81 if (array_key_exists('hyperise_field_desc', $_POST)) { 82 update_post_meta( 83 $post_id, 84 '_hyperise_meta_desc', 85 $_POST['hyperise_field_desc'] 86 ); 87 } 60 88 } 61 89 … … 67 95 add_meta_box( 68 96 'hyperise_add_post', 69 __( 'Hyperise Dynamic ImageSettings' ),97 __( 'Hyperise Open Graph Settings' ), 70 98 'hyperise_render_editor_box', 71 99 $post_type … … 78 106 79 107 function hyperise_render_editor_box( $post ) { 80 $value = get_post_meta($post->ID, '_hyperise_meta_key', true); 108 $hash = get_post_meta($post->ID, '_hyperise_meta_hash', true); 109 $title = get_post_meta($post->ID, '_hyperise_meta_title', true); 110 $desc = get_post_meta($post->ID, '_hyperise_meta_desc', true); 81 111 ?> 82 112 <div id="hyperise-docx-uploader" class="status-empty"> 83 <table width="100%"><tr><td width="50%" valign="top"> 84 <p>Step 1: Add Javascript snippet from step 2.1 of the <a href="https://app.hyperise.io/hyper-campaign" target="_blank">Hyper-Campaign</a></p> 85 <TEXTAREA name="hyperise_field" id="hyperise_field" placeholder="" style="width:100%; min-height:400px;"><?php echo $value; ?></TEXTAREA> 86 </td><td width="50%" valign="top"> 87 <p>Step 2: Copy and remove the image tag</p> 88 <img src="<?php echo plugin_dir_url( __FILE__ ) . 'assets/images/cut_image-1.gif';?>" width="50%" /> 89 <p>Step 3: Add the image tag to your content</p> 90 <img src="<?php echo plugin_dir_url( __FILE__ ) . 'assets/images/add_image.gif';?>" width="100%" /> 91 </td></tr></table> 113 <table width="100%"> 114 <tr><td>Image Hash:</td><td><INPUT size="25" name="hyperise_field_hash" id="hyperise_field_hash" placeholder="" value="<?php echo $hash; ?>" /></td></tr> 115 <tr><td>Title:</td><td><INPUT size="50" name="hyperise_field_title" id="hyperise_field_title" placeholder="{{first_name}} this is just for you" value="<?php echo $title; ?>" /></td></tr> 116 <tr><td>Desc:</td><td><INPUT size="50" name="hyperise_field_desc" id="hyperise_field_desc" placeholder="Created just for {{business_name}}" value="<?php echo $desc; ?>" /></td></tr> 117 </table> 92 118 </div> 93 119 <?php … … 96 122 97 123 function hyperise_enrichdata(){ 98 global $post,$EnrichedData,$utm_hyperef; 99 $m_meta_description = get_post_meta($post->ID, '_hyperise_meta_key', true); 100 101 if(isset($_REQUEST['utm_hyperef'])){ 102 $utm_hyperef=sanitize_text_field($_REQUEST['utm_hyperef']); 103 }else if(isset($_REQUEST['email'])){ 104 $utm_hyperef=sanitize_email($_REQUEST['email']); 105 } 106 107 //remove everything before <script 108 if(strpos($m_meta_description,"<script")>0){ 109 $m_meta_description=substr($m_meta_description,strpos($m_meta_description,"<script")); 110 } 111 112 // find the image ID were using in the JS snippet... 113 $imageIDs = preg_match_all('/imageId: [\'"]([^\'"]+)[\'"].*/i', $m_meta_description, $matches); 114 $imageID = $matches[1][0]; 115 116 // find the image template were using in the JS snippet... 117 $image_templates = preg_match_all('/imageTemplate: [\'"]([^\'"]+)[\'"].*/i', $m_meta_description, $Template_matches); 118 $image_template = $Template_matches[1][0]; 119 120 if(stristr($utm_hyperef,"_") && !stristr($utm_hyperef,"@")){ 121 $data_source_id=substr($utm_hyperef,0,strpos($utm_hyperef,"_")); 122 }else{ 123 //lets find IF we're using a sheet in the JS snippet... 124 $image_templates = preg_match_all('/sourceId: ([^\,]+)/i', $m_meta_description, $Template_matches); 125 $data_source_id = $Template_matches[1][0]; 126 } 127 128 //Enrich the data based on Hyperise Ref... 129 if(strlen($data_source_id)>0 && !stristr($utm_hyperef,"@")){ 130 //need to preg_match this... 131 if(stristr($utm_hyperef,"_")){ 132 $row_id=substr($utm_hyperef,strpos($utm_hyperef,"_")+1); 133 }else{ 134 $row_id=$utm_hyperef; 135 } 136 $json_data = wp_remote_get('https://hyperise.com/test/combinedapi.php?image_template='.$image_template.'&row_id='.$row_id.'&data_source_id='.$data_source_id); 137 $image_url="https://img.hyperise.io/i/$image_template/sheet-$data_source_id/row-$row_id.png"; 138 }else{ 139 //need to preg_match this... 140 $json_data = wp_remote_get('https://hyperise.com/test/image_api.php?image_template='.$image_template.'&utm_hyperef='.$utm_hyperef); 141 $image_url="https://img.hyperise.io/i/$image_template.png?email=$utm_hyperef"; 142 } 143 $EnrichedData=json_decode($json_data[body]); 144 $EnrichedData->data_source_id=$data_source_id; 145 $EnrichedData->image_template=$image_template; 146 $EnrichedData->image_url=$image_url; 147 $EnrichedData->row_id=$row_id; 148 $EnrichedData->imageID=$imageID; 149 $EnrichedData->m_meta_description=$m_meta_description; 150 $EnrichedData->m_meta_title=$post->post_title; 151 $EnrichedData->m_meta_title=str_replace("{{first_name}}",$EnrichedData->first_name,$EnrichedData->m_meta_title); 152 $EnrichedData->m_meta_title=str_replace("{{first_name}}",$EnrichedData->first_name,$EnrichedData->m_meta_title); 153 $EnrichedData->m_meta_title=str_replace("{{last_name}}",$EnrichedData->last_name,$EnrichedData->m_meta_title); 154 $EnrichedData->m_meta_title=str_replace("{{job_title}}",$EnrichedData->job_title,$EnrichedData->m_meta_title); 155 $EnrichedData->m_meta_title=str_replace("{{business_name}}",$EnrichedData->business_name,$EnrichedData->m_meta_title); 156 $EnrichedData->m_meta_title=str_replace("{{website}}",$EnrichedData->website,$EnrichedData->m_meta_title); 157 $EnrichedData->utm_hyperef=$utm_hyperef; 158 124 global $post,$EnrichedData,$utm_hyperef; 125 //$m_meta_description = get_post_meta($post->ID, '_hyperise_meta_key', true); 126 127 $meta_image_hash = get_post_meta($post->ID, '_hyperise_meta_hash', true); 128 129 if(strlen(trim($meta_image_hash))>0) { 130 $meta_title = get_post_meta($post->ID, '_hyperise_meta_title', true); 131 $meta_description = get_post_meta($post->ID, '_hyperise_meta_desc', true); 132 133 if(isset($_GET['utm_hyperef'])){ 134 $utm_hyperef=sanitize_text_field($_GET['utm_hyperef']); 135 }else if(isset($_GET['email'])){ 136 $utm_hyperef=sanitize_email($_GET['email']); 137 } 138 139 $image_template = $meta_image_hash; 140 141 if(strlen($meta_title)>0){ 142 $page_title = $meta_title; 143 }else{ 144 $page_title = $post->post_title; 145 } 146 147 if(strlen($meta_description)>0){ 148 $page_desc= $meta_description; 149 }else{ 150 if( has_excerpt() ){ 151 $page_desc=get_the_excerpt(); 152 } else { 153 $page_desc=substr(strip_tags($post->post_content),0,100); 154 } 155 } 156 157 if(stristr($utm_hyperef,"_") && !stristr($utm_hyperef,"@")){ 158 $data_source_id=substr($utm_hyperef,0,strpos($utm_hyperef,"_")); 159 } 160 161 //Enrich the data based on Hyperise Ref... 162 if(strlen($data_source_id)>0 && !stristr($utm_hyperef,"@")){ 163 //need to preg_match this... 164 if(stristr($utm_hyperef,"_")){ 165 $row_id=substr($utm_hyperef,strpos($utm_hyperef,"_")+1); 166 }else{ 167 $row_id=$utm_hyperef; 168 } 169 $json_data = wp_remote_get('https://hyperise.com/test/combinedapi.php?image_template='.$image_template.'&row_id='.$row_id.'&data_source_id='.$data_source_id); 170 $image_url="https://img.hyperise.io/i/$image_template/sheet-$data_source_id/row-$row_id.png"; 171 }else{ 172 //need to preg_match this... 173 $json_data = wp_remote_get('https://hyperise.com/test/image_api.php?image_template='.$image_template.'&utm_hyperef='.$utm_hyperef); 174 $image_url="https://img.hyperise.io/i/$image_template.png?email=$utm_hyperef"; 175 } 176 $EnrichedData=json_decode($json_data[body]); 177 $EnrichedData->data_source_id=$data_source_id; 178 $EnrichedData->image_template=$image_template; 179 $EnrichedData->image_url=$image_url; 180 $EnrichedData->row_id=$row_id; 181 $EnrichedData->imageID=$imageID; 182 $EnrichedData->m_meta_snippet=esc_attr(get_option( 'hyperise-snippet' )); 183 $EnrichedData->m_meta_title=$page_title; 184 $EnrichedData->m_meta_title=str_replace("{{first_name}}",$EnrichedData->first_name,$EnrichedData->m_meta_title); 185 $EnrichedData->m_meta_title=str_replace("{{first_name}}",$EnrichedData->first_name,$EnrichedData->m_meta_title); 186 $EnrichedData->m_meta_title=str_replace("{{last_name}}",$EnrichedData->last_name,$EnrichedData->m_meta_title); 187 $EnrichedData->m_meta_title=str_replace("{{job_title}}",$EnrichedData->job_title,$EnrichedData->m_meta_title); 188 $EnrichedData->m_meta_title=str_replace("{{business_name}}",$EnrichedData->business_name,$EnrichedData->m_meta_title); 189 $EnrichedData->m_meta_title=str_replace("{{website}}",$EnrichedData->website,$EnrichedData->m_meta_title); 190 $EnrichedData->m_meta_desc=$page_desc; 191 $EnrichedData->m_meta_desc=str_replace("{{first_name}}",$EnrichedData->first_name,$EnrichedData->m_meta_desc); 192 $EnrichedData->m_meta_desc=str_replace("{{first_name}}",$EnrichedData->first_name,$EnrichedData->m_meta_desc); 193 $EnrichedData->m_meta_desc=str_replace("{{last_name}}",$EnrichedData->last_name,$EnrichedData->m_meta_desc); 194 $EnrichedData->m_meta_desc=str_replace("{{job_title}}",$EnrichedData->job_title,$EnrichedData->m_meta_desc); 195 $EnrichedData->m_meta_desc=str_replace("{{business_name}}",$EnrichedData->business_name,$EnrichedData->m_meta_desc); 196 $EnrichedData->m_meta_desc=str_replace("{{website}}",$EnrichedData->website,$EnrichedData->m_meta_desc); 197 198 $EnrichedData->utm_hyperef=$utm_hyperef; 199 } 159 200 return $EnrichedData; 160 201 } … … 163 204 164 205 function hyperise_opengraphsingle(){ 165 if ( is_single() || is_page() ) { 206 if ( is_single() || is_page() && (isset($_GET['utm_hyperef']) || isset($_GET['email']))) { 207 166 208 global $post,$EnrichedData; 167 209 … … 170 212 } 171 213 172 echo $EnrichedData->m_meta_ description;214 echo $EnrichedData->m_meta_snippet; 173 215 174 216 //Replace the page content personalisation tags with enriched data... … … 204 246 ob_start(); 205 247 ob_end_clean(); 206 207 if($EnrichedData-> yoastInstalled <> true){248 249 if($EnrichedData->seoInstalled <> true AND $EnrichedData->image_template<>"" AND $_GET['utm_hyperef']<>""){ 208 250 209 251 //Set the OpenGraph Tags, based on the personalised data we have... 210 echo '<title>',$post->post_title,'</title>'; 211 echo '<meta property="og:site_name" content="',bloginfo('name'),'"/>'; 212 echo '<meta property="og:url" content="',the_permalink(),'?utm_hyperef=',$EnrichedData->utm_hyperef,'"/>'; 213 echo '<meta property="og:title" content="',$post->post_title,'"/>'; 214 if( has_excerpt() ){ 215 echo '<meta property="og:description" content="',get_the_excerpt(),'"/>'; 216 } else { 217 echo '<meta property="og:description" content="',substr(strip_tags($post->post_content),0,100),'"/>'; 218 } 219 if(strlen($EnrichedData->utm_hyperef)>0 && strlen($EnrichedData->image_template)>0){ 220 echo '<meta property="og:image" content="',$EnrichedData->image_url,'"/>'; 221 }else{ 222 echo '<meta property="og:image" content="',esc_attr(get_option( 'default-image' )),'"/>'; 223 } 224 } 225 } 226 } 252 echo '<title>',$EnrichedData->m_meta_title,'</title>'; 253 echo '<meta property="og:site_name" content="',bloginfo('name'),'"/>'; 254 echo '<meta property="og:url" content="',the_permalink(),'?utm_hyperef=',$EnrichedData->utm_hyperef,'"/>'; 255 echo '<meta property="og:title" content="',$EnrichedData->m_meta_title,'"/>'; 256 echo '<meta property="og:description" content="',$EnrichedData->m_meta_desc,'"/>'; 257 echo '<meta property="og:image" content="',$EnrichedData->image_url,'"/>'; 258 } 259 } 260 } 261 262 227 263 228 264 … … 236 272 237 273 function hyperise_opengraph_options(){ 238 register_setting( 'meta-data', ' default-image' );274 register_setting( 'meta-data', 'hyperise-snippet' ); 239 275 } 240 276 241 277 function hyperise_opengraphsingle_menu_page(){ ?> 242 278 <div class="wrap"> 243 <h1 class="ogtitle">H YPERISE OpenGraph/OG Tags Settings</h1>279 <h1 class="ogtitle">Hyperise website content and OpenGraph personalisation setup</h1> 244 280 <form class="ogdata" action="options.php" method="post"> 245 281 <?php settings_fields( 'meta-data' ); ?> 246 282 <?php do_settings_sections( 'hyperise_opengraphsingle_menu' ); ?> 247 <h3> Default Image URL </h3> 248 <p class="ogpara">If the page doesn't contain any dynamic images then this image will be used instead.</p> 249 <input type="text" name="default-image" value="<?php echo esc_attr( get_option( 'default-image' )); ?>"> 283 <h3>Hyperise Snippet</h3> 284 <p class="ogpara">Add your Hyperise Snippet below. If you're unsure where to get your Hyperise snippet from, checkout the <a href="https://support.hyperise.com/website-personalisation/1-adding-hyperise-snippet-to-your-website" target="_blanks">Adding the Hyperise snippet to your website Guide</a>.</p> 285 <textarea id="w3review" name="hyperise-snippet" rows="5" cols="100"> 286 <?php echo esc_attr( get_option( 'hyperise-snippet' )); ?> 287 </textarea> 288 289 <p>To enable OpenGraph personalisation, edit the post/page and set the title, description and image.</p> 290 250 291 <?php submit_button(); ?> 251 292 </form>
Note: See TracChangeset
for help on using the changeset viewer.