Advertisement
arie_cristianD

class.jnews-meta-header

Sep 7th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author : Jegtheme
  4.  */
  5.  
  6. class JNews_Meta_Header {
  7.  
  8.     /**
  9.      * @var JNews_Meta_Header
  10.      */
  11.     private static $instance;
  12.  
  13.     /**
  14.      * @var JNews_Meta_Facebook
  15.      */
  16.     private $facebook_meta;
  17.  
  18.     /**
  19.      * @var JNews_Meta_Twitter
  20.      */
  21.     private $twitter_meta;
  22.  
  23.     /**
  24.      * @return JNews_Meta_Header
  25.      */
  26.     public static function getInstance() {
  27.         if ( null === static::$instance ) {
  28.             static::$instance = new static();
  29.         }
  30.         return static::$instance;
  31.     }
  32.  
  33.     private function __construct() {
  34.         add_action( 'wp', array( $this, 'instantiate_post' ), 1 );
  35.         add_action( 'after_setup_theme', array( $this, 'load_metabox' ) );
  36.         add_action( 'wp_head', array( $this, 'generate_social_meta' ), 1 );
  37.     }
  38.  
  39.     public function load_metabox() {
  40.         if ( class_exists( 'VP_Metabox' ) ) {
  41.             global $pagenow;
  42.             if ( $pagenow === 'post.php' || $pagenow === 'post-new.php' || ! is_admin() ) {
  43.                 new VP_Metabox( JNEWS_META_HEADER_DIR . '/metabox/social-meta.php' );
  44.             }
  45.         }
  46.     }
  47.  
  48.     public function instantiate_post() {
  49.         require_once 'class.jnews-meta-abstract.php';
  50.         require_once 'class.jnews-meta-facebook.php';
  51.         require_once 'class.jnews-meta-twitter.php';
  52.  
  53.         $post_id = get_the_ID();
  54.  
  55.         $this->facebook_meta = new JNews_Meta_Facebook( $post_id );
  56.         $this->twitter_meta  = new JNews_Meta_Twitter( $post_id );
  57.     }
  58.  
  59.     public function generate_social_meta() {
  60.         if ( ! is_admin() && $this->facebook_meta instanceof JNews_Meta_Facebook && $this->twitter_meta instanceof JNews_Meta_Facebook ) {
  61.             // Language is required for gettext.
  62.             // Without it, 'gettext' or '_' will cause error instead of translate text.
  63.             // phpcs:disable WordPress.PHP.DiscouragedPHPFunctions
  64.             $locale      = function_exists( 'jnews_get_locale' ) ? jnews_get_locale() : get_locale();
  65.             $prev_locale = getenv( 'LC_ALL' );
  66.             putenv( 'LC_ALL=' . $locale );
  67.  
  68.             $this->facebook_meta->render_meta();
  69.             $this->twitter_meta->render_meta();
  70.  
  71.             $this->add_fb_app_id();
  72.  
  73.             // Revert Language.
  74.             if ( $prev_locale ) {
  75.                 putenv( 'LC_ALL=' . $prev_locale );
  76.             } else {
  77.                 putenv( 'LC_ALL' );
  78.             }
  79.             // phpcs:enable WordPress.PHP.DiscouragedPHPFunctions
  80.         }
  81.     }
  82.  
  83.     public function add_fb_app_id() {
  84.         $id = jnews_get_option( 'social_meta_fb_app_id', '' );
  85.  
  86.         if ( ! empty( $id ) ) {
  87.             echo '<meta property="fb:app_id" content="' . $id . '">';
  88.         }
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement