Plugin Directory

Changeset 2016610


Ignore:
Timestamp:
01/22/2019 02:55:33 AM (7 years ago)
Author:
alexmacarthur
Message:

Update to version 3.4.0.

Location:
complete-open-graph
Files:
46 added
6 edited

Legend:

Unmodified
Added
Removed
  • complete-open-graph/trunk/complete-open-graph.php

    r1910099 r2016610  
    11<?php
    22/**
    3 * Plugin Name: Complete Open Graph
    4 * Description: Simple, comprehensive, highly customizable Open Graph management.
    5 * Version: 3.3.1
    6 * Author: Alex MacArthur
    7 * Author URI: https://macarthur.me
    8 * License: GPLv2 or later
    9 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
    10 */
     3 * Plugin Name: Complete Open Graph
     4 * Description: Simple, comprehensive, highly customizable Open Graph management.
     5 * Version: 3.4.0
     6 * Author: Alex MacArthur
     7 * Author URI: https://macarthur.me
     8 * License: GPLv2 or later
     9 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
     10 *
     11 * @package CompleteOpenGraph
     12 */
    1113
    1214namespace CompleteOpenGraph;
    1315
    14 if ( !defined( 'WPINC' ) ) {
    15   die;
     16if ( ! defined( 'WPINC' ) ) {
     17    die;
    1618}
    1719
    18 require_once(trailingslashit(ABSPATH) . 'wp-admin/includes/plugin.php');
     20require_once( trailingslashit( ABSPATH ) . 'wp-admin/includes/plugin.php' );
    1921
    20 $realpath = trailingslashit(realpath(dirname(__FILE__)));
     22define( 'COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX', 'complete_open_graph' );
     23define( 'COMPLETE_OPEN_GRAPH_OPTIONS_SHORT_PREFIX', 'complete_open_graph' );
     24define( 'COMPLETE_OPEN_GRAPH_ADMIN_SETTINGS_PAGE_SLUG', 'complete_open_graph' );
     25define( 'COMPLETE_OPEN_GRAPH_PLUGIN_DATA', get_plugin_data( __DIR__ . '/complete-open-graph.php' ) );
     26define( 'COMPLETE_OPEN_GRAPH_REAL_PATH', trailingslashit( realpath( dirname( __FILE__ ) ) ) );
    2127
    22 require_once $realpath . 'src/Filters.php';
    23 require_once $realpath . 'src/Settings.php';
    24 require_once $realpath . 'src/Metabox.php';
    25 require_once $realpath . 'src/Generator.php';
    26 require_once $realpath . 'src/Support.php';
     28require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/Generator.php';
     29require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/Utilities.php';
     30require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/Field.php';
    2731
     32/**
     33 * Responsible for registering global assets and requiring code.
     34 */
    2835class App {
    2936
    30   private static $instance;
    31     public $controllers = array();
    32     protected static $plugin_data = null;
    33   protected static $options_prefix = 'complete_open_graph';
    34   protected static $admin_settings_page_slug = 'complete_open_graph';
    35   protected static $options_short_prefix = 'cog';
    36     protected static $post_decorator = null;
     37    /**
     38     * Initialize the plugin.
     39     *
     40     * @return object App Instance of class.
     41     */
     42    public static function go() {
     43        $GLOBALS[ __CLASS__ ] = new self;
     44        return $GLOBALS[ __CLASS__ ];
     45    }
    3746
    38   public static function go() {
    39         $GLOBALS[__CLASS__] = new self;
    40         return $GLOBALS[__CLASS__];
    41   }
     47    /**
     48     * Instatiate necessary classes, enqueue admin scripts.
     49     */
     50    public function __construct() {
     51        require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/hooks/generate-open-graph-markup.php';
     52        require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/hooks/content-filters.php';
     53        require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/hooks/settings.php';
     54        require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/hooks/metabox.php';
     55        require_once COMPLETE_OPEN_GRAPH_REAL_PATH . 'src/hooks/support.php';
     56        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles_and_scripts' ) );
     57    }
    4258
    43   /**
    44    * Instatiate necessary classes, enqueue admin scripts.
    45    */
    46   public function __construct() {
    47         self::$plugin_data = get_plugin_data(__DIR__ . '/complete-open-graph.php');
     59    /**
     60     * Delete global and post/page data.
     61     *
     62     * @return void
     63     */
     64    public static function delete_options_and_meta() {
     65        global $wpdb;
     66        delete_option( COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX );
     67        $wpdb->delete( $wpdb->prefix . 'postmeta', array( 'meta_key' => COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX ) );
     68    }
    4869
    49     $this->controllers['Settings'] = new Settings;
    50     $this->controllers['Metabox'] = new Metabox;
    51     $this->controllers['Generator'] = new Generator;
    52     $this->controllers['Filters'] = new Filters;
    53     $this->controllers['Support'] = new Support;
    54 
    55     add_action( 'admin_enqueue_scripts', array($this, 'enqueue_styles_and_scripts' ));
    56   }
    57 
    58   /**
    59    * Delete global and post/page data.
    60    *
    61    * @return void
    62    */
    63   public static function delete_options_and_meta() {
    64     global $wpdb;
    65     delete_option(self::$options_prefix);
    66     $wpdb->delete( $wpdb->prefix . 'postmeta', array( 'meta_key' => self::$options_prefix) );
    67   }
    68 
    69   /**
    70    * Enqueue global admin scripts & styles.
    71    *
    72    * @return void
    73    */
    74   public function enqueue_styles_and_scripts() {
    75     wp_enqueue_style( 'complete-open-graph', plugin_dir_url( __FILE__ ) . 'src/assets/css/style.css', array(), self::$plugin_data['Version']);
    76     wp_enqueue_script( 'complete-open-graph', plugin_dir_url( __FILE__ ) . 'src/assets/js/scripts.js', array('jquery'), self::$plugin_data['Version'], true );
    77   }
     70    /**
     71     * Enqueue global admin scripts & styles.
     72     *
     73     * @return void
     74     */
     75    public function enqueue_styles_and_scripts() {
     76        wp_enqueue_style( 'complete-open-graph', plugin_dir_url( __FILE__ ) . 'src/assets/css/style.css', array(), COMPLETE_OPEN_GRAPH_PLUGIN_DATA['Version'] );
     77        wp_enqueue_script( 'complete-open-graph', plugin_dir_url( __FILE__ ) . 'src/assets/js/scripts.js', array( 'jquery' ), COMPLETE_OPEN_GRAPH_PLUGIN_DATA['Version'], true );
     78    }
    7879}
    7980
    8081App::go();
    8182
    82 //-- On uninstallation, delete plugin-related database stuffs.
    83 register_uninstall_hook( __FILE__, array('\CompleteOG\App', 'delete_options_and_meta') );
     83// -- On uninstallation, delete plugin-related database stuffs.
     84register_uninstall_hook( __FILE__, array( '\CompleteOG\App', 'delete_options_and_meta' ) );
  • complete-open-graph/trunk/composer.json

    r1910099 r2016610  
    55  "minimum-stability": "stable",
    66  "license": "GPL-2.0",
    7   "version": "3.3.1",
     7  "version": "3.4.0",
    88  "authors": [
    99    {
     
    2727  },
    2828  "require-dev": {
    29     "phpunit/phpunit": "^6.5"
     29        "phpunit/phpunit": "^6.5",
     30    "squizlabs/php_codesniffer": "^3.4"
    3031  }
    3132}
  • complete-open-graph/trunk/readme.txt

    r1910100 r2016610  
    66Requires at least: 3.9
    77Requires PHP: 5.6
    8 Tested up to: 4.9.7
    9 Stable tag: 3.3.1
     8Tested up to: 5.0.3
     9Stable tag: 3.4.0
    1010License: GPLv2 or later
    1111License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    5353* twitter:url
    5454* twitter:site
     55* profile:first_name (Currently only displayed on individual author pages.)
     56* profile:last_name (Currently only displayed on individual author pages.)
    5557
    5658= Default Settings =
     
    291293* Improve the logic (and respective efficiency) of determining how image meta tags are generated.
    292294
     295= 3.4.0 =
     296* Improve code organization under the hood.
     297* Display "profile:first_name" and "profile:last_name" meta tags on author pages.
     298
    293299== Feedback ==
    294300
  • complete-open-graph/trunk/src/Generator.php

    r1901559 r2016610  
    33namespace CompleteOpenGraph;
    44
    5 class Generator extends App {
     5class Generator {
    66
    7   public function __construct() {
    8     add_action('wp_head', array($this, 'generate_open_graph_markup'));
    9         add_filter('language_attributes', array($this, 'add_open_graph_prefix'), 10, 2);
    10     }
     7    /**
     8     * Get the values for each OG attribute, based on priority & existence of values.
     9     *
     10     * @return array Open Graph values
     11     */
     12    public static function getOpenGraphValues() {
     13        $frontPageID = (int) get_option( 'page_on_front' );
     14        $fields      = Utilities::getFields();
    1115
    12   /**
    13    * Add Open Graph prefixes to <html> tag.
    14    *
    15    * @param string $output A space-separated list of language attributes.
    16    * @param string $doctype The type of html document (xhtml|html).
    17    */
    18   public function add_open_graph_prefix( $output, $doctype ) {
    19         if(!apply_filters(self::$options_prefix . '_maybe_enable', true)) return $output;
    20 
    21     return $output . ' prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# website: http://ogp.me/ns/website#"';
    22     }
    23 
    24   /**
    25    * Generate the Open Graph markup.
    26    *
    27    * @return void
    28    */
    29   public function generate_open_graph_markup() {
    30         if(!apply_filters(self::$options_prefix . '_maybe_enable', true)) return;
    31 
    32     echo "\n\n<!-- Open Graph data is managed by Alex MacArthur's Complete Open Graph plugin. (v" . self::$plugin_data['Version'] . ") -->\n";
    33     echo "<!-- https://wordpress.org/plugins/complete-open-graph/ -->\n";
    34 
    35         $startTime = microtime(true);
    36 
    37     foreach($this->get_open_graph_values() as $key => $data) {
    38 
    39             if(empty($data['value'])) continue;
    40 
    41             //-- @todo Move this into process_content?
    42       $content = preg_replace( "/\r|\n/", "", $data['value']);
    43             $content = htmlentities($content, ENT_QUOTES, 'UTF-8', false);
    44 
    45             if($data['attribute'] === 'property') {
    46                 ?><meta property="<?php echo $key; ?>" content="<?php echo $content; ?>" /><?php
    47                 echo "\n";
    48                 continue;
    49             }
    50 
    51             if($data['attribute'] === 'name') {
    52                 ?><meta name="<?php echo $key; ?>" content="<?php echo $content; ?>" /><?php
    53                 echo "\n";
    54                 continue;
    55             }
    56     }
    57 
    58     echo "<!-- End Complete Open Graph. | " . (microtime(true) - $startTime) . "s -->\n\n";
    59   }
    60 
    61   /**
    62    * Get a specific value for an Open Graph attribute.
    63    * If progression is given, it will assign the first value that exists.
    64    *
    65    * @param  string $field_name  Name of the attribute.
    66    * @param  array  $progression Array of possible values, in order of priority.
    67      * @param  array    $protectedKeys Prevents specified keys from being removed duing useGlobal.
    68    * @return string
    69    */
    70   public function get_processed_value($field_name, $progression = array(), $protectedKeys = array()) {
    71 
    72         //-- Check for explicit option to use global options, or if it's an archive page.
    73         $useGlobal =
    74             (
    75                 Utilities::get_option('force_all') === 'on' ||
    76                 Utilities::get_option( $field_name . '_force' ) === 'on' ||
    77                 (is_home() || is_archive())
    78             );
    79 
    80     if($useGlobal) {
    81             $value = Utilities::process_content(Utilities::get_option($field_name));
    82 
    83             //-- Remove non-protected items before we tack on our global value.
    84             //-- This way, we can have a fallback system in place in case a global value is empty.
    85             $progression = $this->get_only_protected_values($progression, $protectedKeys);
    86 
    87             array_unshift($progression, $value);
    88 
    89       return apply_filters(
    90                 self::$options_prefix . '_processed_value',
    91                 Utilities::get_cascaded_value($progression),
    92                 $field_name);
     16        // -- Filter for filtering specific fields.
     17        foreach ( $fields as $key => $item ) {
     18            $value = isset( $fields[ $key ]['get_value'] ) ? $fields[ $key ]['get_value']() : "";
     19            $fields[ $key ]['value'] = apply_filters( COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX . '_' . $key, $value, $key );
    9320        }
    9421
    95         return apply_filters(
    96             self::$options_prefix . '_processed_value',
    97             Utilities::get_cascaded_value($progression),
    98             $field_name
    99         );
    100 
     22        return apply_filters( COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX . '_all_data', $fields );
    10123    }
    102 
    103     /**
    104      * Ideally, this would be array_filter(), but was causing PHP fallback issues.
    105      *
    106      * @param array $progression
    107      * @param array $protectedKeys
    108      * @return array
    109      */
    110     public function get_only_protected_values($progression, $protectedKeys) {
    111         $protectedValues = array();
    112 
    113         foreach($protectedKeys as $key) {
    114             if(!isset($progression[$key])) continue;
    115             $protectedValues[] = $progression[$key];
    116         }
    117 
    118         return $protectedValues;
    119     }
    120 
    121   /**
    122    * Get the values for each OG attribute, based on priority & existence of values.
    123    *
    124    * @return array Open Graph values
    125    */
    126   public function get_open_graph_values() {
    127         $frontPageID = (int) get_option('page_on_front');
    128 
    129     $data = array(
    130       'og:site_name' => array(
    131         'attribute' => 'property',
    132         'value' => $site_name = get_bloginfo('name')
    133       ),
    134 
    135       'og:url' => array(
    136         'attribute' => 'property',
    137                 'value' => $this->get_processed_value( 'og:url',
    138                     array(
    139                         get_permalink(Utilities::get_post_decorator()->ID),
    140                         get_bloginfo('url')
    141                     ),
    142                     array(1)
    143                 )
    144       ),
    145 
    146       'og:locale' => array(
    147         'attribute' => 'property',
    148         'value' => get_locale()
    149       ),
    150 
    151       'og:description' => array(
    152         'attribute' => 'property',
    153         'value' => $description = $this->get_processed_value( 'og:description',
    154           array(
    155             Utilities::get_post_option('og:description'),
    156             Utilities::get_post_decorator()->post_excerpt,
    157                         Utilities::get_post_decorator()->post_content,
    158             Utilities::get_option('og:description'),
    159                         get_bloginfo('description')
    160                     ),
    161                     array(3, 4)
    162         )
    163       ),
    164 
    165       'og:title' => array(
    166         'attribute' => 'property',
    167         'value' => $theTitle = $this->get_processed_value( 'og:title',
    168           array(
    169             Utilities::get_post_option('og:title'),
    170             get_the_title(),
    171             Utilities::get_option('og:title'),
    172             $site_name
    173                     ),
    174                     array(2, 3)
    175         )
    176       ),
    177 
    178       'og:type' => array(
    179         'attribute' => 'property',
    180         'value' => $this->get_processed_value( 'og:type',
    181           array(
    182             Utilities::get_post_option('og:type'),
    183             is_single() ? 'article' : '',
    184                         Utilities::get_option('og:type'),
    185                         'website'
    186                     ),
    187                     array(2, 3)
    188         )
    189       ),
    190 
    191       //-- Might be a string, might be an ID. Will be filtered to account for both.
    192       'og:image' => array(
    193         'attribute' => 'property',
    194         'value' => $image = $this->get_processed_value( 'og:image',
    195           array(
    196             Utilities::get_post_option('og:image'),
    197             get_post_thumbnail_id(Utilities::get_post_decorator()->ID),
    198             Utilities::get_first_image(),
    199             Utilities::get_option('og:image'),
    200                         !empty($frontPageID) && has_post_thumbnail($frontPageID)
    201                             ? get_post_thumbnail_id( $frontPageID )
    202                             : false
    203                     ),
    204                     array(3, 4)
    205         )
    206       ),
    207 
    208       'og:image:width' => array(
    209         'attribute' => 'property',
    210         'value' => ''
    211       ),
    212 
    213       'og:image:height' => array(
    214         'attribute' => 'property',
    215         'value' => ''
    216       ),
    217 
    218       'fb:admins' => array(
    219         'attribute' => 'property',
    220         'value' => Utilities::get_option('fb:admins')
    221       ),
    222 
    223       'fb:app_id' => array(
    224         'attribute' => 'property',
    225         'value' => Utilities::get_option('fb:app_id')
    226       ),
    227 
    228       'twitter:card' => array(
    229         'attribute' => 'name',
    230         'value' => $this->get_processed_value('twitter:card',
    231           array(
    232             Utilities::get_post_option('twitter:card'),
    233                         Utilities::get_option('twitter:card'),
    234                         'summary'
    235                     ),
    236                     array(2)
    237         )
    238       ),
    239 
    240       'twitter:creator' => array(
    241         'attribute' => 'name',
    242         'value' => $this->get_processed_value('twitter:creator',
    243           array(
    244             Utilities::get_post_option('twitter:creator'),
    245             Utilities::get_option('twitter:creator')
    246           )
    247         )
    248       ),
    249 
    250       'twitter:site' => array(
    251         'attribute' => 'name',
    252         'value' => Utilities::get_option('twitter:site')
    253       ),
    254 
    255       'twitter:title' => array(
    256         'attribute' => 'name',
    257         'value' => $theTitle
    258       ),
    259 
    260       'twitter:image' => array(
    261         'attribute' => 'name',
    262         'value' => $image
    263       ),
    264 
    265       'twitter:description' => array(
    266         'attribute' => 'name',
    267         'value' => $this->get_processed_value( 'twitter:description',
    268           array(
    269             Utilities::get_post_option('twitter:description'),
    270             Utilities::get_post_decorator()->post_excerpt,
    271             Utilities::get_post_decorator()->post_content,
    272             Utilities::get_option('twitter:description'),
    273             $description
    274           )
    275         )
    276       )
    277     );
    278 
    279     //-- Filter for filtering specific fields.
    280     foreach($data as $key=>$item) {
    281       $data[$key]['value'] = apply_filters(self::$options_prefix . '_' . $key, $data[$key]['value'], $key);
    282     }
    283 
    284     return apply_filters(self::$options_prefix . '_all_data', $data);
    285   }
    28624}
  • complete-open-graph/trunk/src/PostDecorator.php

    r1780101 r2016610  
    55class PostDecorator {
    66
    7   /**
    8   * WP_Post object.
    9   *
    10   * @var obj
    11   */
    12   public $post;
     7    /**
     8    * WP_Post object.
     9    *
     10    * @var obj
     11    */
     12    public $post;
    1313
    14   /**
    15   * Constructs decorator based on WP_Post object.
    16   *
    17   * @param WP_Post|null $post
    18   */
    19   public function __construct($post = null) {
    20     $this->post = is_null($post) ? $GLOBALS['post'] : $post;
    21   }
     14    /**
     15    * Constructs decorator based on WP_Post object.
     16    *
     17    * @param WP_Post|null $post
     18    */
     19    public function __construct( $post = null ) {
     20        $this->post = is_null( $post ) ? $GLOBALS['post'] : $post;
     21    }
    2222
    23   /**
    24   * Returns false if property doesn't exist.
    25   *
    26   * @param  string $key
    27   * @return mixed
    28   */
    29   public function __get($key) {
    30     if(!isset($this->post->$key)) {
    31       return null;
    32     }
     23    /**
     24    * Returns false if property doesn't exist.
     25    *
     26    * @param  string $key
     27    * @return mixed
     28    */
     29    public function __get( $key ) {
     30        if ( ! isset( $this->post->$key ) ) {
     31            return null;
     32        }
    3333
    34     return $this->post->$key;
    35   }
     34        return $this->post->$key;
     35    }
    3636}
  • complete-open-graph/trunk/src/Utilities.php

    r1896779 r2016610  
    55require_once 'PostDecorator.php';
    66
    7 class Utilities extends App {
    8 
    9   public static function get_current_post_type() {
    10     global $post, $typenow, $current_screen;
    11 
    12     if ( $post && $post->post_type ) {
    13       return $post->post_type;
    14     }
    15 
    16     elseif ( $typenow ) {
    17       return $typenow;
    18     }
    19 
    20     elseif ( $current_screen && $current_screen->post_type ) {
    21       return $current_screen->post_type;
    22     }
    23 
    24     elseif ( isset( $_REQUEST['post_type'] ) ) {
    25       return sanitize_key( $_REQUEST['post_type'] );
    26     }
    27 
    28     elseif ( isset( $_REQUEST['post'] ) ) {
    29       return get_post_type( $_REQUEST['post'] );
    30     }
    31 
    32     return null;
    33   }
    34 
    35   /**
    36    * Gets serialized settings in options table.
    37    *
    38    * @return array
    39    */
    40   public static function get_options() {
    41         return get_option(self::$options_prefix);
    42   }
    43 
    44   /**
    45    * Gets specific option value.
    46    *
    47    * @param  string $key Option key
    48    * @return string
    49    */
    50   public static function get_option($key) {
    51     if(isset(self::get_options()[$key])) {
    52       return self::get_options()[$key];
    53     }
    54 
    55     return false;
    56   }
    57 
    58   /**
    59    * Returns instance of PostDecorator, creates one if not defined.
    60    *
    61    * @return obj
    62    */
    63   public static function get_post_decorator() {
    64     global $post;
    65 
    66     if(is_null(self::$post_decorator)) {
    67       self::$post_decorator = new PostDecorator($post);
    68     }
    69 
    70     return self::$post_decorator;
    71   }
    72 
    73   /**
    74    * Gets serialized options for individual post/page.
    75    *
    76    * @return array
    77    */
    78   public static function get_post_options() {
    79         $post_options = get_post_meta(self::get_post_decorator()->ID, self::$options_prefix);;
    80 
    81     if(empty($post_options)) {
    82       return array();
    83     }
    84 
    85     return $post_options[0];
    86   }
    87 
    88   /**
    89    * Gets value for specific post/page option.
    90    *
    91    * @param  string $key Field key
    92    * @return string|bool
    93    */
    94   public static function get_post_option($key) {
    95     $post_options = self::get_post_options();
    96     return !empty($post_options[$key]) ? $post_options[$key] : false;
    97   }
    98 
    99   /**
    100    * Gets full name of particular field, with prefix appended.
    101    *
    102    * @param  string $name Name of field
    103    * @return string
    104    */
    105   public static function get_field_name($name) {
    106     return self::$options_prefix . '[' . $name . ']';
    107   }
    108 
    109   /**
    110    * Gets the first image that appears on the post/page.
    111    *
    112    * @return string|bool
    113    */
    114   public static function get_first_image() {
    115     $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', self::get_post_decorator()->post_content, $matches);
    116     return !empty($matches[1][0]) ? $matches[1][0] : false;
    117   }
    118 
    119   /**
    120    * Strips all tags from a string of text.
    121    *
    122    * @param  string text
    123    * @return string
    124    */
    125   public static function strip_all_tags($text) {
    126     if(!$text) return $text;
    127 
    128     $text = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $text);
    129     $text = preg_replace('#<style(.*?)>(.*?)</style>#is', '', $text);
    130 
    131     return strip_tags($text);
    132   }
    133 
    134   /**
    135    * Do all the things for formatting a piece of OG content.
    136    *
    137    * @param  string $content
    138    * @return string
    139    */
    140   public static function process_content($content) {
    141     $value = strip_shortcodes($content);
    142     $value = self::strip_all_tags($value);
    143     $value = trim($value);
    144     $value = substr($value, 0, 300);
    145     return $value;
     7class Utilities {
     8
     9    public static function getFields() {
     10        return include 'fields.php';
     11    }
     12
     13    public static function get_current_post_type() {
     14        global $post, $typenow, $current_screen;
     15
     16        if ( $post && $post->post_type ) {
     17            return $post->post_type;
     18        } elseif ( $typenow ) {
     19            return $typenow;
     20        } elseif ( $current_screen && $current_screen->post_type ) {
     21            return $current_screen->post_type;
     22        } elseif ( isset( $_REQUEST['post_type'] ) ) {
     23            return sanitize_key( $_REQUEST['post_type'] );
     24        } elseif ( isset( $_REQUEST['post'] ) ) {
     25            return get_post_type( $_REQUEST['post'] );
     26        }
     27
     28        return null;
     29    }
     30
     31    /**
     32     * Gets serialized settings in options table.
     33     *
     34     * @return array
     35     */
     36    public static function get_options() {
     37        return get_option( COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX );
     38    }
     39
     40    /**
     41     * Gets specific option value.
     42     *
     43     * @param  string $key Option key
     44     * @return string
     45     */
     46    public static function get_option( $key ) {
     47        if ( isset( self::get_options()[ $key ] ) ) {
     48            return self::get_options()[ $key ];
     49        }
     50
     51        return false;
     52    }
     53
     54    /**
     55     * Returns instance of PostDecorator, creates one if not defined.
     56     *
     57     * @return obj
     58     */
     59    public static function get_post_decorator() {
     60        global $post;
     61        return new PostDecorator( $post );
     62    }
     63
     64    /**
     65     * Gets serialized options for individual post/page.
     66     *
     67     * @return array
     68     */
     69    public static function get_post_options() {
     70        $post_options = get_post_meta( self::get_post_decorator()->ID, COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX );
     71
     72        if ( empty( $post_options ) ) {
     73            return array();
     74        }
     75
     76        return $post_options[0];
     77    }
     78
     79    /**
     80     * Gets value for specific post/page option.
     81     *
     82     * @param  string $key Field key
     83     * @return string|bool
     84     */
     85    public static function get_post_option( $key ) {
     86        $post_options = self::get_post_options();
     87        return ! empty( $post_options[ $key ] ) ? $post_options[ $key ] : false;
     88    }
     89
     90    /**
     91     * Gets full name of particular field, with prefix appended.
     92     *
     93     * @param  string $name Name of field
     94     * @return string
     95     */
     96    public static function get_field_name( $name ) {
     97        return COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX . '[' . $name . ']';
     98    }
     99
     100    /**
     101     * Gets the first image that appears on the post/page.
     102     *
     103     * @return string|bool
     104     */
     105    public static function get_first_image() {
     106        $output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', self::get_post_decorator()->post_content, $matches );
     107        return ! empty( $matches[1][0] ) ? $matches[1][0] : false;
     108    }
     109
     110    /**
     111     * Strips all tags from a string of text.
     112     *
     113     * @param  string text
     114     * @return string
     115     */
     116    public static function strip_all_tags( $text ) {
     117        if ( ! $text ) {
     118            return $text;
     119        }
     120
     121        $text = preg_replace( '#<script(.*?)>(.*?)</script>#is', '', $text );
     122        $text = preg_replace( '#<style(.*?)>(.*?)</style>#is', '', $text );
     123
     124        return strip_tags( $text );
     125    }
     126
     127    /**
     128     * Do all the things for formatting a piece of OG content.
     129     *
     130     * @param  string $content
     131     * @return string
     132     */
     133    public static function process_content( $content ) {
     134        $value = strip_shortcodes( $content );
     135        $value = self::strip_all_tags( $value );
     136        $value = trim( $value );
     137        $value = substr( $value, 0, 300 );
     138        return $value;
    146139    }
    147140
     
    152145     * @return string
    153146     */
    154     public static function get_cascaded_value($progression) {
    155         if(empty($progression)) return '';
    156 
    157         foreach ($progression as $progressionValue) {
    158             if(!empty($progressionValue)) {
    159                 return Utilities::process_content($progressionValue);
     147    public static function get_cascaded_value( $progression ) {
     148        if ( empty( $progression ) ) {
     149            return '';
     150        }
     151
     152        foreach ( $progression as $progressionValue ) {
     153            if ( ! empty( $progressionValue ) ) {
     154                return self::process_content( $progressionValue );
    160155            }
    161156        }
     
    164159    }
    165160
     161    /**
     162     * Get a specific value for an Open Graph attribute.
     163     * If progression is given, it will assign the first value that exists.
     164     *
     165     * @param  string $field_name  Name of the attribute.
     166     * @param  array  $progression Array of possible values, in order of priority.
     167     * @param  array  $protectedKeys Prevents specified keys from being removed duing useGlobal.
     168     * @return string
     169     */
     170    public static function get_processed_value( $field_name, $progression = array(), $protectedKeys = array() ) {
     171
     172        // -- Check for explicit option to use global options, or if it's an archive page.
     173        $useGlobal =
     174            (
     175                self::get_option( 'force_all' ) === 'on' ||
     176                self::get_option( $field_name . '_force' ) === 'on' ||
     177                ( is_home() || is_archive() )
     178            );
     179
     180        if ( $useGlobal ) {
     181            $value = self::process_content( self::get_option( $field_name ) );
     182
     183            // -- Remove non-protected items before we tack on our global value.
     184            // -- This way, we can have a fallback system in place in case a global value is empty.
     185            $progression = self::get_only_protected_values( $progression, $protectedKeys );
     186
     187            array_unshift( $progression, $value );
     188
     189            return apply_filters(
     190                COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX . '_processed_value',
     191                self::get_cascaded_value( $progression ),
     192                $field_name
     193            );
     194        }
     195
     196        return apply_filters(
     197            COMPLETE_OPEN_GRAPH_OPTIONS_PREFIX . '_processed_value',
     198            self::get_cascaded_value( $progression ),
     199            $field_name
     200        );
     201
     202    }
     203
     204    /**
     205     * Ideally, this would be array_filter(), but was causing PHP fallback issues.
     206     *
     207     * @param array $progression
     208     * @param array $protectedKeys
     209     * @return array
     210     */
     211    public static function get_only_protected_values( $progression, $protectedKeys ) {
     212        $protectedValues = array();
     213
     214        foreach ( $protectedKeys as $key ) {
     215            if ( ! isset( $progression[ $key ] ) ) {
     216                continue;
     217            }
     218            $protectedValues[] = $progression[ $key ];
     219        }
     220
     221        return $protectedValues;
     222    }
     223
    166224}
Note: See TracChangeset for help on using the changeset viewer.