<?php defined('ABSPATH') || exit(); global $wpdb; define( 'SOCIALMARK_UPLOAD_BASE', $socialmark_upload_dir['basedir'] ); $socialmark_og_row = $wpdb->get_results("SELECT id FROM {$wpdb->prefix}sm_images", ARRAY_A); if (in_array('wordpress-seo/wp-seo.php', apply_filters('active_plugins', get_option('active_plugins'))) || in_array('wordpress-seo-premium/wp-seo-premium.php', apply_filters('active_plugins', get_option('active_plugins')))) { function socialmark_yoast_support(){ if (!is_front_page() && !is_home()) { function socialmark_change_wpseo_og_img_url($url) { $post_id = get_the_ID(); if (is_singular('post') || is_singular('page')) { if (get_post_type($post_id) === "post" && get_option('disable_socialmark') !== "on" && get_option('disable_socialmark_posts') !== "on") { $post_id = get_the_ID(); //return str_replace(wp_get_attachment_url(get_post_thumbnail_id($post_id)), socialmark_generate_og_image($post_id), $url); $socialmark_url_for_yoast = socialmark_generate_og_image($post_id); echo socialmark_twitter_yoast_support($socialmark_url_for_yoast); return $socialmark_url_for_yoast; }elseif (get_post_type($post_id) === "page" && get_option('disable_socialmark') !== "on" && get_option('disable_socialmark_pages') !== "on") { $post_id = get_the_ID(); //return str_replace(wp_get_attachment_url(get_post_thumbnail_id($post_id)), socialmark_generate_og_image($post_id), $url); $socialmark_url_for_yoast = socialmark_generate_og_image($post_id); echo socialmark_twitter_yoast_support($socialmark_url_for_yoast); return $socialmark_url_for_yoast; }else{ $socialmark_url_for_yoast = get_the_post_thumbnail_url($post_id,'full'); echo socialmark_twitter_yoast_support($socialmark_url_for_yoast); return $socialmark_url_for_yoast; } }else{ $socialmark_url_for_yoast = get_the_post_thumbnail_url($post_id,'full'); echo socialmark_twitter_yoast_support($socialmark_url_for_yoast); return $socialmark_url_for_yoast; } } add_filter('wpseo_opengraph_image', 'socialmark_change_wpseo_og_img_url'); } } add_action('wp_head', 'socialmark_yoast_support', 1); function socialmark_twitter_yoast_support($socialmark_url_for_yoast){ return ' <!-- Social Image Generated by: SocialMark Plugin --> <meta name="twitter:image" content="' . $socialmark_url_for_yoast . '" /> <!-- SocialMark Plugin --> '; } } else if (in_array('seo-by-rank-math/rank-math.php', apply_filters('active_plugins', get_option('active_plugins')))) { function socialmark_rankmath_support() { if (!is_front_page() && !is_home()) { function socialmark_change_rankmath_og_img_url($attachment_url) { $post_id = get_the_ID(); if (is_singular('post') || is_singular('page')) { if (get_post_type($post_id) === "post" && get_option('disable_socialmark') !== "on" && get_option('disable_socialmark_posts') !== "on") { return $attachment_url = socialmark_generate_og_image($post_id); }elseif (get_post_type($post_id) === "page" && get_option('disable_socialmark') !== "on" && get_option('disable_socialmark_pages') !== "on") { return $attachment_url = socialmark_generate_og_image($post_id); }else{ return get_the_post_thumbnail_url($post_id,'full'); } }else{ return get_the_post_thumbnail_url($post_id,'full'); } } add_filter("rank_math/opengraph/facebook/image", 'socialmark_change_rankmath_og_img_url'); add_filter("rank_math/opengraph/twitter/image", 'socialmark_change_rankmath_og_img_url'); } } add_action('wp_head', 'socialmark_rankmath_support', 1); } else { if (!empty($socialmark_og_row)) { add_action('wp_head', 'socialmark_frontend_header', 1); } } function socialmark_frontend_header() { if(!is_front_page() && !is_home()) { if ( is_singular( 'post' ) ) { $post_id = get_the_ID(); if (get_post_type($post_id) === "post" && get_option('disable_socialmark') !== "on" && get_option('disable_socialmark_posts') !== "on") { $socialmark_generated_image=socialmark_generate_og_image($post_id); echo ' <!-- Social Image Generated by: SocialMark Plugin --> <meta property="og:type" content="article" /> <meta property="og:title" content="' . esc_html(get_the_title()) . '" /> <meta property="og:url" content="'.get_permalink().'" /> <meta property="og:image" content="' . $socialmark_generated_image . '" /> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:title" content="'.esc_html( get_the_title() ).'" /> <meta name="twitter:image" content="' . $socialmark_generated_image . '" /> <!-- SocialMark Plugin --> '; } } if ( is_singular( 'page' ) ) { $post_id = get_the_ID(); if (get_post_type($post_id) === "page" && get_option('disable_socialmark') !== "on" && get_option('disable_socialmark_pages') !== "on") { $socialmark_generated_image = socialmark_generate_og_image($post_id); echo ' <!-- Social Image Generated by: SocialMark Plugin --> <meta property="og:type" content="article" /> <meta property="og:title" content="' . esc_html(get_the_title()) . '" /> <meta property="og:url" content="' . get_permalink() . '" /> <meta property="og:image" content="' . $socialmark_generated_image . '" /> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:title" content="' . esc_html(get_the_title()) . '" /> <meta name="twitter:image" content="' . $socialmark_generated_image . '" /> <!-- End: SocialMark Plugin --> '; } } } } function socialmark_generate_og_image($post_id) { if(get_attached_file(get_post_thumbnail_id($post_id))==""){ return ""; } if (get_post_meta($post_id, 'socialmark_og_image_url', true) && get_post_meta($post_id, 'socialmark_og_image_url', true) !== "") { $socialmark_og_image = SOCIALMARK_UPLOAD . '/' . esc_attr(get_post_meta($post_id, 'socialmark_og_image_url', true)); $socialmark_og_image_url = SOCIALMARK_UPLOAD_URL . '/' . esc_attr(get_post_meta($post_id, 'socialmark_og_image_url', true)); if (file_exists($socialmark_og_image)) { $GLOBALS['socialmark_global_image']=$socialmark_og_image_url; list($GLOBALS['socialmark_global_image_width'], $GLOBALS['socialmark_global_image_height']) = getimagesize($socialmark_og_image); return $socialmark_og_image_url; } else { $socialmark_og_image_raw=socialmark_regenerate_og_image($post_id); if($socialmark_og_image_raw!='attachment'){ $socialmark_og_image = SOCIALMARK_UPLOAD . '/' . $socialmark_og_image_raw; $socialmark_og_image_url = SOCIALMARK_UPLOAD_URL . '/' . $socialmark_og_image_raw; }else{ $socialmark_og_image = get_attached_file(get_post_thumbnail_id($post_id));; $socialmark_og_image_url = wp_get_attachment_url(get_post_thumbnail_id($post_id)); } $GLOBALS['socialmark_global_image']=$socialmark_og_image_url; list($GLOBALS['socialmark_global_image_width'], $GLOBALS['socialmark_global_image_height']) = getimagesize($socialmark_og_image); return $socialmark_og_image_url; } } else { $socialmark_og_image_raw=socialmark_regenerate_og_image($post_id); if($socialmark_og_image_raw!='attachment'){ $socialmark_og_image = SOCIALMARK_UPLOAD . '/' . $socialmark_og_image_raw; $socialmark_og_image_url = SOCIALMARK_UPLOAD_URL . '/' . $socialmark_og_image_raw; }else{ $socialmark_og_image = get_attached_file(get_post_thumbnail_id($post_id));; $socialmark_og_image_url = wp_get_attachment_url(get_post_thumbnail_id($post_id)); } $GLOBALS['socialmark_global_image']=$socialmark_og_image_url; list($GLOBALS['socialmark_global_image_width'], $GLOBALS['socialmark_global_image_height']) = getimagesize($socialmark_og_image); return $socialmark_og_image_url; } } function socialmark_regenerate_og_image($post_id) { global $wpdb; if (get_option('default_socialmark_overlay')) { $socialmark_de_ov = esc_attr(get_option('default_socialmark_overlay')); $socialmark_og_row = $wpdb->get_results("SELECT id, image_link FROM {$wpdb->prefix}sm_images where id=$socialmark_de_ov", ARRAY_A); $socialmark_overlay_image = str_replace(get_bloginfo('url')."/wp-content/uploads",SOCIALMARK_UPLOAD_BASE,$socialmark_og_row[0]['image_link']); if (get_post_thumbnail_id($post_id)) { $socialmark_main_image = get_attached_file(get_post_thumbnail_id($post_id)); }else{ if(wp_get_attachment_url(get_post_thumbnail_id($post_id))){ //return wp_get_attachment_url(get_post_thumbnail_id($post_id)); return 'attachment'; } } }else{ if(wp_get_attachment_url(get_post_thumbnail_id($post_id))){ // return wp_get_attachment_url(get_post_thumbnail_id($post_id)); return 'attachment'; } } $socialmark_overlay_position = get_option('default_socialmark_position'); //will be updated later // if(isset($socialmark_overlay_image) && !file_exists($socialmark_overlay_image)){ // if(wp_get_attachment_url(get_post_thumbnail_id($post_id))){ // //return wp_get_attachment_url(get_post_thumbnail_id($post_id)); // return 'attachment'; // } // } if (isset($socialmark_overlay_image) && isset($socialmark_main_image) && isset($socialmark_overlay_position)) { $socialmark_src_url = $socialmark_overlay_image; $socialmark_dest_url = $socialmark_main_image; $socialmark_position = $socialmark_overlay_position; $socialmark_src_type = getimagesize($socialmark_src_url); $socialmark_src_type = $socialmark_src_type['mime']; $socialmark_dest_type = getimagesize($socialmark_dest_url); $socialmark_dest_type = $socialmark_dest_type['mime']; switch ($socialmark_src_type) { case 'image/jpeg': $socialmark_src = imagecreatefromjpeg($socialmark_src_url); break; case 'image/gif': //Top-right $socialmark_src = imagecreatefromgif($socialmark_src_url); break; case 'image/png': //Top-right $socialmark_src = imagecreatefrompng($socialmark_src_url); break; } switch ($socialmark_dest_type) { case 'image/jpeg': $socialmark_dest1 = imagecreatefromjpeg($socialmark_dest_url); break; case 'image/gif': //Top-right $socialmark_dest1 = imagecreatefromgif($socialmark_dest_url); break; case 'image/png': //Top-right $socialmark_dest1 = imagecreatefrompng($socialmark_dest_url); break; } list($socialmark_width, $socialmark_height) = getimagesize($socialmark_dest_url); $socialmark_extra = 0; if (imagesx($socialmark_dest1) > 1200) { $socialmark_d_ratio = imagesy($socialmark_dest1) / imagesx($socialmark_dest1); $socialmark_n_width = 1200; $socialmark_n_height = 1200 * $socialmark_d_ratio; if ($socialmark_n_height > 630) { $socialmark_extra = ($socialmark_n_height - 630) / 2; } } else { list($socialmark_n_width, $socialmark_n_height) = getimagesize($socialmark_dest_url); if ($socialmark_n_width > 500 && ($socialmark_n_width / $socialmark_n_height) < 1.90 && $socialmark_n_width > $socialmark_n_height) { //$socialmark_extra = (((($socialmark_n_height * 1.9 - $socialmark_n_width) * 100) / $socialmark_n_width) * ($socialmark_n_height / 100)) / 2; $socialmark_extra = ($socialmark_n_height-($socialmark_n_width*0.525))/2; } } $socialmark_dest = imagecreatetruecolor($socialmark_n_width, $socialmark_n_height); imagecopyresampled($socialmark_dest, $socialmark_dest1, 0, 0, 0, 0, $socialmark_n_width, $socialmark_n_height, $socialmark_width, $socialmark_height); list($socialmark_src_w, $socialmark_src_h) = getimagesize($socialmark_src_url); $socialmark_name = time().$post_id; if (imagesx($socialmark_dest) < imagesx($socialmark_src)) { $socialmark_ratio = imagesy($socialmark_src) / imagesx($socialmark_src); $socialmark_src_w = imagesx($socialmark_dest); $socialmark_src_h = imagesx($socialmark_dest) * $socialmark_ratio; $socialmark_old_x = imagesx($socialmark_src); $socialmark_old_y = imagesy($socialmark_src); $socialmark_nw_img = ImageCreateTrueColor($socialmark_src_w, $socialmark_src_h); imagealphablending($socialmark_nw_img, false); imagesavealpha($socialmark_nw_img, true); $socialmark_transparent = imagecolorallocatealpha($socialmark_nw_img, 255, 255, 255, 127); imagefilledrectangle($socialmark_nw_img, 0, 0, $socialmark_src_w, $socialmark_src_h, $socialmark_transparent); imagecopyresampled($socialmark_nw_img, $socialmark_src, 0, 0, 0, 0, $socialmark_src_w, $socialmark_src_h, $socialmark_old_x, $socialmark_old_y); $socialmark_src = $socialmark_nw_img; } else if (imagesy($socialmark_dest) < imagesy($socialmark_src)) { $socialmark_ratio = imagesx($socialmark_src) / imagesy($socialmark_src); $socialmark_src_w = imagesy($socialmark_dest) * $socialmark_ratio; $socialmark_src_h = imagesy($socialmark_dest); $socialmark_old_x = imagesx($socialmark_src); $socialmark_old_y = imagesy($socialmark_src); $socialmark_nw_img = ImageCreateTrueColor($socialmark_src_w, $socialmark_src_h); imagealphablending($socialmark_nw_img, false); imagesavealpha($socialmark_nw_img, true); $socialmark_transparent = imagecolorallocatealpha($socialmark_nw_img, 255, 255, 255, 127); imagefilledrectangle($socialmark_nw_img, 0, 0, $socialmark_src_w, $socialmark_src_h, $socialmark_transparent); imagecopyresampled($socialmark_nw_img, $socialmark_src, 0, 0, 0, 0, $socialmark_src_w, $socialmark_src_h, $socialmark_old_x, $socialmark_old_y); $socialmark_src = $socialmark_nw_img; } switch ($socialmark_position) { case 5: //Center $socialmark_xaxis = (imagesx($socialmark_dest) / 2) - (imagesx($socialmark_src) / 2); $socialmark_yaxis = (imagesy($socialmark_dest) / 2) - (imagesy($socialmark_src) / 2); break; case 9: //Center-bottom $socialmark_xaxis = (imagesx($socialmark_dest) / 2) - (imagesx($socialmark_src) / 2); $socialmark_yaxis = (imagesy($socialmark_dest) - imagesy($socialmark_src)) - $socialmark_extra; break; } socialmark_imagecopymerge_alpha($socialmark_dest, $socialmark_src, $socialmark_xaxis, $socialmark_yaxis, 0, 0, $socialmark_src_w, $socialmark_src_h, 100); // imagejpeg($socialmark_dest, SOCIALMARK_UPLOAD . '/' . $socialmark_name . '.jpg'); // $socialmark_og_image_url = $socialmark_name . '.jpg'; if (imagesx($socialmark_dest) <= imagesy($socialmark_dest)) { $socialmark_background = "#FFFFFF"; $socialmark_im = imagecreatetruecolor(1200, 630); // fill the image with the color you want $color = '0x' . ltrim($socialmark_background, '#'); imagefilledrectangle($socialmark_im, 0, 0, 1200, 630, hexdec($color)); $socialmark_re_width = imagesx($socialmark_dest); $socialmark_re_height = imagesy($socialmark_dest); $socialmark_width_re_ratio = $socialmark_width / $socialmark_width; // $resized_width = 500; //suppose 500 is max width or height // $resized_height = 500/$ratio; //} //else { $socialmark_resized_width = 630 * $socialmark_width_re_ratio; $socialmark_resized_height = 630; //$image = imagecreatefromjpeg('shirt.jpg'); $sm_resized_image = imagecreatetruecolor($socialmark_resized_width, $socialmark_resized_height); $left = (1200 - $socialmark_resized_width) / 2; imagecopyresampled($sm_resized_image, $socialmark_dest, 0, 0, 0, 0, $socialmark_resized_width, $socialmark_resized_height, $socialmark_re_width, $socialmark_re_height); // set the headers and output the image imagecopymerge($socialmark_im, $sm_resized_image, $left, 0, 0, 0, $socialmark_resized_width, $socialmark_resized_height, 100); imagejpeg($socialmark_im, SOCIALMARK_UPLOAD . '/' . $socialmark_name . '.jpg'); $socialmark_og_image_url = $socialmark_name . '.jpg'; }else{ $socialmark_crop_array = array('x' =>0 , 'y' => $socialmark_extra, 'width' => imagesx($socialmark_dest), 'height'=> imagesy($socialmark_dest)-$socialmark_extra*2); imagejpeg(imagecrop($socialmark_dest, $socialmark_crop_array), SOCIALMARK_UPLOAD . '/' . $socialmark_name . '.jpg'); $socialmark_og_image_url = $socialmark_name . '.jpg'; } update_post_meta( $post_id, 'socialmark_og_image_url', sanitize_text_field($socialmark_og_image_url) ); imagedestroy($socialmark_src); imagedestroy($socialmark_dest); imagedestroy($socialmark_dest1); //return SOCIALMARK_UPLOAD_URL . '/' . $socialmark_og_image_url; return $socialmark_og_image_url; }else{ if (wp_get_attachment_url(get_post_thumbnail_id($post_id))) { //return wp_get_attachment_url(get_post_thumbnail_id($post_id)); return "attachment"; } } } function socialmark_imagecopymerge_alpha($socialmark_dst_im, $socialmark_src_im, $socialmark_dst_x, $socialmark_dst_y, $socialmark_src_x, $socialmark_src_y, $socialmark_src_w, $socialmark_src_h, $socialmark_pct) { $socialmark_cut = imagecreatetruecolor($socialmark_src_w, $socialmark_src_h); imagecopy($socialmark_cut, $socialmark_dst_im, 0, 0, $socialmark_dst_x, $socialmark_dst_y, $socialmark_src_w, $socialmark_src_h); imagecopy($socialmark_cut, $socialmark_src_im, 0, 0, $socialmark_src_x, $socialmark_src_y, $socialmark_src_w, $socialmark_src_h); imagecopymerge($socialmark_dst_im, $socialmark_cut, $socialmark_dst_x, $socialmark_dst_y, 0, 0, $socialmark_src_w, $socialmark_src_h, $socialmark_pct); } function socialmark_callback($buffer) { global $socialmark_global_image; global $socialmark_global_image_width; global $socialmark_global_image_height; if($socialmark_global_image!=""){ $buffer = preg_replace('/\<meta property="og:image" content="(.*?)"/si', '<meta property="og:image" content="' . $socialmark_global_image . '"', $buffer); $buffer = preg_replace('/\<meta property="og:image:width" content="(.*?)"/si', '<meta property="og:image:width" content="' . $socialmark_global_image_width . '"', $buffer); $buffer = preg_replace('/\<meta property="og:image:height" content="(.*?)"/si', '<meta property="og:image:height" content="' . $socialmark_global_image_height . '"', $buffer); $buffer = preg_replace('/\<meta name="twitter:image" content="(.*?)"/si', '<meta name="twitter:image" content="' . $socialmark_global_image . '"', $buffer); $buffer = preg_replace('/\<meta property="og:image:url" content="(.*?)"/si', '<meta property="twitter:image" content="' . $socialmark_global_image . '"', $buffer); $buffer = preg_replace('/\<meta property="og:image:secure_url" content="(.*?)"/si', '<meta property="twitter:image" content="' . $socialmark_global_image . '"', $buffer); $buffer = preg_replace('/\<meta property="twitter:image" content="(.*?)"/si', '<meta property="twitter:image" content="' . $socialmark_global_image . '"', $buffer); } return $buffer; } function socialmark_buffer_start() { ob_start("socialmark_callback"); } function socialmark_buffer_end() { ob_end_flush(); } function socialmark_test_global(){ global $socialmark_global_image; return array_key_exists('socialmark_global_image', $GLOBALS) ? true : false; } function socialmark_jetpack_image( $tags ) { // Remove default blank image from Jetpack global $socialmark_global_image; global $socialmark_global_image_width; global $socialmark_global_image_height; unset( $tags['og:image'] ); unset( $tags['og:image:width'] ); unset( $tags['og:image:height'] ); $tags['og:image'] = esc_url( $socialmark_global_image ); $tags['og:image:width'] = $socialmark_global_image_width ; $tags['og:image:height'] = $socialmark_global_image_height ; return $tags; } if (get_option('disable_socialmark_force') !== "on") { if (socialmark_test_global()===true) { add_action('after_setup_theme', 'socialmark_buffer_start'); add_action('wp_head', 'socialmark_buffer_end'); if (in_array('jetpack/jetpack.php', apply_filters('active_plugins', get_option('active_plugins'))) || in_array('jetpack/jetpack.php', apply_filters('active_plugins', get_option('active_plugins')))) { add_filter('jetpack_open_graph_tags', 'socialmark_jetpack_image'); add_filter('jetpack_disable_twitter_cards', '__return_true', 99); } } }