/components/facebook/loader.php

https://github.com/mahype/SeoPress · PHP · 168 lines · 136 code · 27 blank · 5 comment · 18 complexity · 184034e25d05e1615f5a8019d445938c MD5 · raw file

  1. <?php
  2. require_once( dirname(__FILE__) . '/open-graph.php' );
  3. function sp_fp_add_meta(){
  4. if( $_SERVER['HTTP_USER_AGENT'] == 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' ) {
  5. global $post;
  6. if( $post->ID != '' ){
  7. $og = new FB_Open_Graph();
  8. $post_meta = get_post_meta( $post->ID, 'sp_post_metabox', TRUE );
  9. $fb_title = $post_meta['fb_title'];
  10. $fb_description = $post_meta['fb_description'];
  11. $fb_image = $post_meta['fb_image'];
  12. $fb_type = $post_meta['fb_type'];
  13. if( $fb_image == 'featured_image' ){
  14. $ptn_id = get_post_thumbnail_id( $post->ID );
  15. $fb_image_src = wp_get_attachment_url( $ptn_id );
  16. }else if( $fb_image == 'post_text' ){
  17. $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches );
  18. $fb_image_src = $matches [1] [0];
  19. }
  20. $args = array(
  21. 'title' => $fb_title,
  22. 'description' => $fb_description,
  23. 'type' => $fb_type,
  24. 'image' => $fb_image_src
  25. );
  26. $og->set_page_data( $args );
  27. echo $og->get_header_data();
  28. }
  29. }
  30. }
  31. add_action( 'sp_insert_meta', 'sp_fp_add_meta' );
  32. function sp_fb_page_meta_box( &$tabs ){
  33. $html = '<p class="sp_metabox_description">' . __( 'Leave fields blank if you want to use standard WordPress values.', 'seopress' ) . '</p>';
  34. $select_fb_image = new TK_WP_FORM_SELECT( 'fb_image', 'sp_post_metabox', 'seopress_fb_image' );
  35. $select_fb_image->add_option( __('featured image', 'seopress' ), 'featured_image' );
  36. $select_fb_image->add_option( __('post text', 'seopress' ), 'post_text' );
  37. $select_fb_type = new TK_WP_FORM_SELECT( 'fb_type', 'sp_post_metabox', 'seopress_fb_type' );
  38. // $select_fb_type->add_option( __('Test', 'seopress' ), 'test' );
  39. // $select_fb_type->add_option( __('Test2', 'seopress' ), 'test2' );
  40. $html.= '<table class="form-table">
  41. <tbody>
  42. <tr>
  43. <td width="200" valign="top"><label for="seopress_fb_title">' . __( 'Title', 'seopress' ) . ':</label></td>
  44. <td>' . tk_wp_form_textfield( 'fb_title', 'sp_post_metabox', 'seopress_fb_title', ' style="width:99%"' ) . $title_field . '</td>
  45. </tr>
  46. <tr>
  47. <td valign="top"><label for="seopress_fb_description">' . __( 'Description', 'seopress' ) . ':</label></td>
  48. <td>' . tk_wp_form_textfield( 'fb_description', 'sp_post_metabox', 'seopress_fb_description', ' style="width:99%"' ) . $description_field . '</td>
  49. </tr>
  50. <tr>
  51. <td valign="top"><label for="seopress_fb_image">' . __( 'Image', 'seopress' ) . ':</label></td>
  52. <td>' . $select_fb_image->get_html() . '</td>
  53. </tr><!--
  54. <tr>
  55. <td valign="top"><label for="seopress_fb_type">' . __( 'Type', 'seopress' ) . ':</label></td>
  56. <td>' . $select_fb_type->get_html() . '</td>
  57. </tr>//-->
  58. ' . $post_metabox_table .'
  59. </tbody>
  60. </table>';
  61. $tabs->add_tab( 'cap_page_facebook', __ ('Facebook', 'seopress'), $html );
  62. }
  63. add_action( 'sp_page_metabox_tabs', 'sp_fb_post_meta_box' );
  64. function sp_fb_post_meta_box( $tabs ){
  65. global $post;
  66. $html = '<p class="sp_metabox_description">' . __( 'Leave fields blank if you want to use standard values.', 'seopress' ) . '</p>';
  67. $select_fb_image = new TK_WP_FORM_SELECT( 'fb_image', 'sp_post_metabox', 'seopress_fb_image' );
  68. $select_fb_image->add_option( __('featured image', 'seopress' ), 'featured_image' );
  69. $select_fb_image->add_option( __('post text', 'seopress' ), 'post_text' );
  70. $select_fb_type = new TK_WP_FORM_SELECT( 'fb_type', 'sp_post_metabox', 'seopress_fb_type' );
  71. // $select_fb_type->add_option( __('Test1', 'seopress' ), 'test' );
  72. // $select_fb_type->add_option( __('Test2', 'seopress' ), 'test2' );
  73. // Preview data
  74. $post_meta = get_post_meta( $post->ID , 'sp_post_metabox' , true );
  75. $preview_image = $post_meta['fb_image'];
  76. if( $preview_image == 'featured_image' ){
  77. $ptn_id = get_post_thumbnail_id( $post->ID );
  78. $preview_image = wp_get_attachment_url( $ptn_id );
  79. }else if( $preview_image == 'post_text' ){
  80. $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches );
  81. $preview_image = $matches [1] [0];
  82. }
  83. if( $preview_image != '' ){
  84. $text_width = '300';
  85. }else{
  86. $text_width = '400';
  87. }
  88. if( $post_meta['fb_title'] != '' ){
  89. $preview_title = $post_meta['fb_title'];
  90. }else{
  91. $preview_title = $post->post_title;
  92. }
  93. if( $post_meta['fb_description'] != '' ){
  94. $preview_desc = $post_meta['fb_description'];
  95. }else{
  96. $preview_desc = substr( strip_tags( $post->post_content ), 0, 300 ) . ' ... ';
  97. }
  98. $preview_url = get_permalink( $post->ID );
  99. $preview_url = str_replace('http://', '', $preview_url );
  100. $html.= '<table class="form-table">
  101. <tbody>
  102. <tr>
  103. <td width="200" valign="top"><label for="seopress_fb_title">' . __( 'Title', 'seopress' ) . ':</label></td>
  104. <td>' . tk_wp_form_textfield( 'fb_title', 'sp_post_metabox', 'seopress_fb_title', ' style="width:99%"' ) . $title_field . '</td>
  105. </tr>
  106. <tr>
  107. <td valign="top"><label for="seopress_fb_description">' . __( 'Description', 'seopress' ) . ':</label></td>
  108. <td>' . tk_wp_form_textfield( 'fb_description', 'sp_post_metabox', 'seopress_fb_description', ' style="width:99%"' ) . $description_field . '</td>
  109. </tr>
  110. <tr>
  111. <td valign="top"><label for="seopress_fb_image">' . __( 'Take image from', 'seopress' ) . ':</label></td>
  112. <td>' . $select_fb_image->get_html() . '</td>
  113. </tr>
  114. <tr>
  115. <td valign="top">' . __( 'Preview:', 'seopress' ) . '</td>
  116. <td>
  117. <div style="border: 1px #000 solid; bgcolor: #FFF; width:96%; height; 100%; padding:1em;">
  118. <div style="width: 400px;">
  119. <img style="max-width:90px; float:left; margin-right: 10px; " src="' . $preview_image . '" />
  120. <div style="float:right; width: ' . $text_width . 'px;">
  121. <strong><div style="color: #3B5998; text-decoration: none; font-size: 11px; font-family: lucida grande,tahoma,verdana,arial,sans-serif; margin-top: -5px;">' . $preview_title . '</div></strong>
  122. <div style="color: #808080; text-decoration: none; font-size: 11px; font-family: lucida grande,tahoma,verdana,arial,sans-serif; line-height: 12px;">' . $preview_desc . '</div>
  123. <div style="clear:both;"></div>
  124. </div>
  125. <div style="clear:both;"></div>
  126. </div>
  127. </div>
  128. </td>
  129. </tr>
  130. <!--
  131. <tr>
  132. <td valign="top"><label for="seopress_fb_type">' . __( 'Type', 'seopress' ) . ':</label></td>
  133. <td>' . $select_fb_type->get_html() . '</td>
  134. </tr>//-->
  135. ' . $post_metabox_table . '
  136. </tbody>
  137. </table>';
  138. $tabs->add_tab( 'cap_post_facebook', __ ('Facebook', 'seopress'), $html );
  139. }
  140. add_action( 'sp_post_metabox_tabs', 'sp_fb_post_meta_box' );
  141. ?>