|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: PRESENTA Open-Graph |
| 4 | +Plugin URI: https://www.presenta.cc/open-graph-wordpress-plugin |
| 5 | +Description: PRESENTA Open-Graph plugin generates social preview images and tags automatically for each post or page. |
| 6 | +Tags: social, social sharing, open graph, social image, twitter card, open graph |
| 7 | +Requires at least: 4.0 |
| 8 | +Version: 1.0.0 |
| 9 | +Author: PRESENTA |
| 10 | +Author URI: https://www.presenta.cc |
| 11 | +License: GPLv2 or later |
| 12 | +*/ |
| 13 | + |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { |
| 15 | + exit; |
| 16 | +} |
| 17 | + |
| 18 | +$PRESENTA_SERVICE_URL = 'https://cloud.preso.cc/v1/url/'; |
| 19 | + |
| 20 | +function presenta_head_meta_data() { |
| 21 | + |
| 22 | + if(is_admin()) return; |
| 23 | + |
| 24 | + if ( is_category() || is_tag() ) return; |
| 25 | + |
| 26 | + global $post, $PRESENTA_SERVICE_URL; |
| 27 | + |
| 28 | + if (empty($post)) return; |
| 29 | + |
| 30 | + $pTemplateID = get_option('presenta_plugin_template_id'); |
| 31 | + $hasYoast = get_option('presenta_plugin_template_yoast'); |
| 32 | + if (empty($pTemplateID)) return; |
| 33 | + |
| 34 | + $post_id = $post->ID; |
| 35 | + $author_id = $post->post_author; |
| 36 | + |
| 37 | + $post_date = get_the_modified_date(); |
| 38 | + $post_author = get_the_author_meta('display_name', $author_id); |
| 39 | + $post_title = wp_strip_all_tags(get_the_title($post_id)); |
| 40 | + $post_excerpt = wp_strip_all_tags(get_the_excerpt($post_id)); |
| 41 | + $post_image = get_the_post_thumbnail_url($post_id); |
| 42 | + $post_url = get_permalink($post_id); |
| 43 | + |
| 44 | + $site_name = get_bloginfo('name'); |
| 45 | + $site_url = site_url(); |
| 46 | + |
| 47 | + if (!is_singular()){ |
| 48 | + $post_title = $site_name; |
| 49 | + } |
| 50 | + |
| 51 | + if(empty($post_image) && !empty($unsplash_topic)){ |
| 52 | + $post_image = "https://source.unsplash.com/random/800x600/?sky"; |
| 53 | + } |
| 54 | + |
| 55 | + $url = $PRESENTA_SERVICE_URL . $pTemplateID; |
| 56 | + $url .= "?title=" . $post_title; |
| 57 | + $url .= "&subtitle=" . $post_date; |
| 58 | + $url .= "&image=" . $post_image; |
| 59 | + |
| 60 | + $output = '<!-- PRESENTA OG start --> |
| 61 | + '; |
| 62 | + |
| 63 | + if($hasYoast != '1'){ |
| 64 | + $output .= '<meta property="og:type" content="website">'; |
| 65 | + $output .= '<meta property="og:title" content="'.$post_title.'">'; |
| 66 | + $output .= '<meta property="og:site_name" content="'.$site_name.'">'; |
| 67 | + $output .= '<meta property="og:description" content="'.$post_excerpt.'">'; |
| 68 | + $output .= '<meta property="og:url" content="'.$post_url.'">'; |
| 69 | + |
| 70 | + $output .= '<meta name="twitter:card" content="summary_large_image" />'; |
| 71 | + $output .= '<meta name="twitter:title" content="'.$post_title.'" />'; |
| 72 | + $output .= '<meta name="twitter:site" content="'.$site_name.'" />'; |
| 73 | + $output .= '<meta name="twitter:description" content="'.$post_excerpt.'" />'; |
| 74 | + $output .= '<meta name="twitter:url" content="'.$post_url.'" />'; |
| 75 | + } |
| 76 | + |
| 77 | + $output .= '<meta name="twitter:image" content="'.$url.'" />'; |
| 78 | + $output .= '<meta property="og:image" content="'.$url.'" /> |
| 79 | + '; |
| 80 | + |
| 81 | + $output .= '<!-- PRESENTA OG end -->'; |
| 82 | + |
| 83 | + echo $output; |
| 84 | + |
| 85 | +} |
| 86 | +add_action('wp_head', 'presenta_head_meta_data', 1); |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +function presenta_plugin_options_validate(){ |
| 99 | + // validate input |
| 100 | +} |
| 101 | + |
| 102 | +function presenta_plugin_section_callback(){ |
| 103 | +} |
| 104 | + |
| 105 | +function presenta_plugin_template_id_callback(){ |
| 106 | + $setting = get_option('presenta_plugin_template_id'); |
| 107 | + ?> |
| 108 | + <input placeholder="i.e. xxxxxxxx:yyyyyyyyy" type="text" name="presenta_plugin_template_id" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>"> |
| 109 | + <?php |
| 110 | +} |
| 111 | + |
| 112 | +function presenta_plugin_template_yoast_callback(){ |
| 113 | + $setting = get_option('presenta_plugin_template_yoast'); |
| 114 | + ?> |
| 115 | + <input type="text" name="presenta_plugin_template_yoast" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>"> |
| 116 | + <?php |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +function presenta_register_settings() { |
| 121 | + add_settings_section( 'presenta_plugin_section', 'Open-Graph image generator for WordPress', 'presenta_plugin_section_callback', 'presenta_plugin_options' ); |
| 122 | + add_settings_field( 'presenta_plugin_template_id', 'Template ID', 'presenta_plugin_template_id_callback', 'presenta_plugin_options', 'presenta_plugin_section' ); |
| 123 | + add_settings_field( 'presenta_plugin_template_yoast', 'Yoast Fix', 'presenta_plugin_template_yoast_callback', 'presenta_plugin_options', 'presenta_plugin_section' ); |
| 124 | + |
| 125 | + register_setting( 'presenta_plugin_options', 'presenta_plugin_section' ); |
| 126 | + register_setting( 'presenta_plugin_options', 'presenta_plugin_template_id' ); //, 'presenta_plugin_options_validate' |
| 127 | + register_setting( 'presenta_plugin_options', 'presenta_plugin_template_yoast' ); //, 'presenta_plugin_options_validate' |
| 128 | +} |
| 129 | +add_action( 'admin_init', 'presenta_register_settings' ); |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +function presenta_render_plugin_setting_panel(){ |
| 140 | + global $PRESENTA_SERVICE_URL; |
| 141 | + ?> |
| 142 | + <h1>PRESENTA Open-Graph</h1> |
| 143 | + <form action="options.php" method="post" class="presenta_form"> |
| 144 | + <?php |
| 145 | + //settings_errors(); |
| 146 | + settings_fields( 'presenta_plugin_options' ); |
| 147 | + do_settings_sections( 'presenta_plugin_options' ); |
| 148 | + submit_button(); |
| 149 | + ?> |
| 150 | + </form> |
| 151 | + |
| 152 | + <p>Choose the template you prefer the most, review the options below, then, Save Changes.</p> |
| 153 | + <p><input type="checkbox" id="presenta_yoast_fix" /> I have Yoast or other SEO plugins active. This option forces PRESENTA handling only the image tag.</p> |
| 154 | + <!--<p><select> |
| 155 | + <option>-- Disabled --</option> |
| 156 | + <option>Sky</option> |
| 157 | + <option>Home</option> |
| 158 | + <option>Tech</option> |
| 159 | + </select> Choose the topic (or disable it) for image fallback (post/page without Featured image) picked randomly from Unsplash.</p> |
| 160 | + --> |
| 161 | + |
| 162 | + <div id="presenta_gallery_container"> |
| 163 | + <div class="presenta_template"> |
| 164 | + <div class="presenta_template_inner"> |
| 165 | + <img src="<?php echo plugin_dir_url( __FILE__ ) . 'none.jpg'; ?>" /> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + |
| 170 | + |
| 171 | + <script> |
| 172 | + |
| 173 | + const src = [ |
| 174 | + {"id": "zGywhb2oJn:mzT3zNoLn"}, |
| 175 | + {"id": "zGywhb2oJn:BEaGHrXOs"}, |
| 176 | + {"id": "zGywhb2oJn:0vx3VZVjP"}, |
| 177 | + {"id": "zGywhb2oJn:MiKViTKFM"}, |
| 178 | + |
| 179 | + {"id": "zGywhb2oJn:Gx5I8aeto"}, |
| 180 | + {"id": "zGywhb2oJn:9CF5pEILq"}, |
| 181 | + {"id": "zGywhb2oJn:6JaJpv7Qo"}, |
| 182 | + {"id": "zGywhb2oJn:JCNwsKA6w"} |
| 183 | + ] |
| 184 | + |
| 185 | + |
| 186 | + <?php $templateID = get_option('presenta_plugin_template_id'); ?> |
| 187 | + const actual = "<?php echo $templateID; ?>" |
| 188 | + |
| 189 | + <?php $yoastFix = get_option('presenta_plugin_template_yoast'); ?> |
| 190 | + const checkYoast = document.querySelector('#presenta_yoast_fix') |
| 191 | + const hasYoast = "<?php echo $yoastFix; ?>" |
| 192 | + if(hasYoast == '1') checkYoast.checked = true |
| 193 | + checkYoast.addEventListener('change', e => { |
| 194 | + const v = e.target.checked |
| 195 | + const field = document.querySelector('[name="presenta_plugin_template_yoast"]') |
| 196 | + field.value = v ? 1 : 0 |
| 197 | + }) |
| 198 | + |
| 199 | + const base = '<?php echo $PRESENTA_SERVICE_URL; ?>' |
| 200 | + |
| 201 | + const wrapper = document.querySelector('#presenta_gallery_container') |
| 202 | + |
| 203 | + src.forEach((t,i) => { |
| 204 | + const el = document.createElement('div') |
| 205 | + el.classList.add('presenta_template') |
| 206 | + |
| 207 | + const inn = document.createElement('div') |
| 208 | + inn.classList.add('presenta_template_inner') |
| 209 | + el.append(inn) |
| 210 | + |
| 211 | + const img = document.createElement('img') |
| 212 | + img.setAttribute('src', base + t.id + '?title=Contrary to popular belief, Lorem Ipsum is not simply random text.&subtitle=January 1, 2022&image=https://images.unsplash.com/photo-1501785888041-af3ef285b470?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=900&q=80') |
| 213 | + inn.append(img) |
| 214 | + |
| 215 | + img.setAttribute('data-id', t.id) |
| 216 | + img.setAttribute('data-index', i+1) |
| 217 | + |
| 218 | + if(t.id == actual) img.classList.add('selected') |
| 219 | + |
| 220 | + wrapper.append(el) |
| 221 | + }) |
| 222 | + |
| 223 | + if(!actual){ |
| 224 | + const first = wrapper.querySelector('.presenta_template_inner img:first-child') |
| 225 | + first.classList.add('selected') |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + wrapper.addEventListener('click', e => { |
| 231 | + const id = e.target.getAttribute('data-id') |
| 232 | + const index = e.target.getAttribute('data-index') |
| 233 | + const field = document.querySelector('[name="presenta_plugin_template_id"]') |
| 234 | + field.value = id |
| 235 | + |
| 236 | + const list = [...wrapper.querySelectorAll('.presenta_template_inner img')] |
| 237 | + list.forEach(d => { |
| 238 | + d.classList.remove('selected') |
| 239 | + }) |
| 240 | + |
| 241 | + list[+index].classList.add('selected') |
| 242 | + |
| 243 | + }) |
| 244 | + |
| 245 | + |
| 246 | + </script> |
| 247 | + <style> |
| 248 | + #presenta_gallery_container * { |
| 249 | + box-sizing: border-box; |
| 250 | + } |
| 251 | + #presenta_gallery_container{ |
| 252 | + display:flex; |
| 253 | + flex-wrap: wrap; |
| 254 | + padding-right: 20px; |
| 255 | + } |
| 256 | + .presenta_template{ |
| 257 | + width: 33.333333%; |
| 258 | + } |
| 259 | + .presenta_template_inner{ |
| 260 | + padding:10px; |
| 261 | + } |
| 262 | + .presenta_template .selected{ |
| 263 | + border:5px solid #1E66A8; |
| 264 | + } |
| 265 | + .presenta_template img{ |
| 266 | + display:block; |
| 267 | + width:100%; |
| 268 | + height:auto; |
| 269 | + box-shadow: 0 0 10px #ccc; |
| 270 | + } |
| 271 | + .presenta_form table{ |
| 272 | + display:none; |
| 273 | + } |
| 274 | + </style> |
| 275 | + <?php |
| 276 | +} |
| 277 | + |
| 278 | + |
| 279 | +function presenta_add_settings_page() { |
| 280 | + add_options_page( 'PRESENTA OG Settings', 'PRESENTA OG', 'manage_options', 'presenta-og-plugin', 'presenta_render_plugin_setting_panel' ); |
| 281 | +} |
| 282 | +add_action( 'admin_menu', 'presenta_add_settings_page' ); |
| 283 | + |
| 284 | + |
| 285 | + |
0 commit comments