Plugin Directory

Changeset 2344005


Ignore:
Timestamp:
07/21/2020 10:03:17 AM (6 years ago)
Author:
qrac
Message:

Update to version 1.0.2 from GitHub

Location:
qroko-blocks
Files:
258 added
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • qroko-blocks/tags/1.0.2/functions/open-graph.php

    r2338987 r2344005  
    11<?php
    2 /**
    3  * Copyright 2010 Scott MacVicar
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  * http://www.apache.org/licenses/LICENSE-2.0
    8  * Unless required by applicable law or agreed to in writing, software
    9  * distributed under the License is distributed on an "AS IS" BASIS,
    10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  * See the License for the specific language governing permissions and
    12  * limitations under the License.
    13  * Original can be found at https://github.com/scottmac/opengraph/blob/master/OpenGraph.php
    14  */
     2// Debug
     3//require __DIR__ . '/../debug/ChromePhp.php';
    154
    16 class OpenGraph implements Iterator {
    17   /**
    18    * There are base schema's based on type, this is just
    19    * a map so that the schema can be obtained
    20    *
    21    */
    22   public static $TYPES = array(
    23     'activity' => array('activity', 'sport'),
    24     'business' => array('bar', 'company', 'cafe', 'hotel', 'restaurant'),
    25     'group' => array('cause', 'sports_league', 'sports_team'),
    26     'organization' => array('band', 'government', 'non_profit', 'school', 'university'),
    27     'person' => array('actor', 'athlete', 'author', 'director', 'musician', 'politician', 'public_figure'),
    28     'place' => array('city', 'country', 'landmark', 'state_province'),
    29     'product' => array('album', 'book', 'drink', 'food', 'game', 'movie', 'product', 'song', 'tv_show'),
    30     'website' => array('blog', 'website'),
    31   );
    32 
    33   /**
    34    * Holds all the Open Graph values we've parsed from a page
    35    *
    36    */
    37   private $_values = array();
    38 
    39   /**
    40    * Fetches a URI and parses it for Open Graph data, returns
    41    * false on error.
    42    *
    43    * @param $URI    URI to page to parse for Open Graph data
    44    * @return OpenGraph
    45    */
    46   static public function fetch($URI) {
    47     $response = wp_remote_get($URI);
    48 
    49     if (!empty($response)) {
    50       return self::_parse($response['body']);
    51     } else {
    52       return false;
    53     }
    54   }
    55 
    56   /**
    57    * Parses HTML and extracts Open Graph data, this assumes
    58    * the document is at least well formed.
    59    *
    60    * @param $HTML    HTML to parse
    61    * @return OpenGraph
    62    */
    63   static private function _parse($HTML) {
    64     $old_libxml_error = libxml_use_internal_errors(true);
    65 
    66     $HTML = mb_convert_encoding($HTML,"HTML-ENTITIES","UTF-8");
    67 
    68     $doc = new DOMDocument();
    69     $doc->loadHTML($HTML);
    70 
    71     libxml_use_internal_errors($old_libxml_error);
    72 
    73     $tags = $doc->getElementsByTagName('meta');
    74     if (!$tags || $tags->length === 0) {
    75       return false;
    76     }
    77 
    78     $page = new self();
    79 
    80     $nonOgDescription = null;
    81 
    82     foreach ($tags AS $tag) {
    83       if ($tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) {
    84         $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
    85         $page->_values[$key] = $tag->getAttribute('content');
    86       }
    87 
    88       // Added this if loop to retrieve description values from sites like the New York Times who have malformed it.
    89       if ($tag ->hasAttribute('value') && $tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) {
    90         $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
    91         $page->_values[$key] = $tag->getAttribute('value');
    92       }
    93 
    94       // Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
    95       if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'description') {
    96         $nonOgDescription = $tag->getAttribute('content');
    97       }
    98     }
    99 
    100     // Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
    101     if (!isset($page->_values['title'])) {
    102       $titles = $doc->getElementsByTagName('title');
    103       if ($titles->length > 0) {
    104         $page->_values['title'] = $titles->item(0)->textContent;
    105       }
    106     }
    107 
    108     if (!isset($page->_values['description']) && $nonOgDescription) {
    109       $page->_values['description'] = $nonOgDescription;
    110     }
    111 
    112     // Fallback to use image_src if ogp::image isn't set.
    113     if (!isset($page->values['image'])) {
    114       $domxpath = new DOMXPath($doc);
    115       $elements = $domxpath->query("//link[@rel='image_src']");
    116 
    117       if ($elements->length > 0) {
    118         $domattr = $elements->item(0)->attributes->getNamedItem('href');
    119         if ($domattr) {
    120           $page->_values['image'] = $domattr->value;
    121           $page->_values['image_src'] = $domattr->value;
    122         }
    123       }
    124     }
    125 
    126     if (empty($page->_values)) { return false; }
    127 
    128     return $page;
    129   }
    130 
    131   /**
    132    * Helper method to access attributes directly
    133    * Example:
    134    * $graph->title
    135    *
    136    * @param $key    Key to fetch from the lookup
    137    */
    138   public function __get($key) {
    139     if (array_key_exists($key, $this->_values)) {
    140       return $this->_values[$key];
    141     }
    142 
    143     if ($key === 'schema') {
    144       foreach (self::$TYPES AS $schema => $types) {
    145         if (array_search($this->_values['type'], $types)) {
    146           return $schema;
    147         }
    148       }
    149     }
    150   }
    151 
    152   /**
    153    * Return all the keys found on the page
    154    *
    155    * @return array
    156    */
    157   public function keys() {
    158     return array_keys($this->_values);
    159   }
    160 
    161   /**
    162    * Helper method to check an attribute exists
    163    *
    164    * @param $key
    165    */
    166   public function __isset($key) {
    167     return array_key_exists($key, $this->_values);
    168   }
    169 
    170   /**
    171    * Will return true if the page has location data embedded
    172    *
    173    * @return boolean Check if the page has location data
    174    */
    175   public function hasLocation() {
    176     if (array_key_exists('latitude', $this->_values) && array_key_exists('longitude', $this->_values)) {
    177       return true;
    178     }
    179 
    180     $address_keys = array('street_address', 'locality', 'region', 'postal_code', 'country_name');
    181     $valid_address = true;
    182     foreach ($address_keys AS $key) {
    183       $valid_address = ($valid_address && array_key_exists($key, $this->_values));
    184     }
    185     return $valid_address;
    186   }
    187 
    188   /**
    189    * Iterator code
    190    */
    191   private $_position = 0;
    192   public function rewind() { reset($this->_values); $this->_position = 0; }
    193   public function current() { return current($this->_values); }
    194   public function key() { return key($this->_values); }
    195   public function next() { next($this->_values); ++$this->_position; }
    196   public function valid() { return $this->_position < sizeof($this->_values); }
    197 }
     5// Composer
     6require __DIR__ . '/../vendor/autoload.php';
     7use Embed\Embed;
    1988
    1999function open_graph() {
     
    20111  $esc_target_url = esc_url($target_url);
    20212
    203   $graph = OpenGraph::fetch($esc_target_url);
     13  $graph = Embed::create($esc_target_url);
    20414
    20515  $og_title = $graph->title;
     
    22939
    23040  // Debug: Chrome
    231   //include '../debug/ChromePhp.php';
     41  //ChromePhp::log(json_encode($graph));
    23242  //ChromePhp::log(json_encode($records));
    23343
  • qroko-blocks/tags/1.0.2/qroko-blocks.php

    r2338987 r2344005  
    66 * Author: qrac
    77 * Author URI: https://qrac.jp/
    8  * Version: 1.0.1
     8 * Version: 1.0.2
    99 * Text Domain: qroko-blocks
    1010 * Domain Path: /languages
  • qroko-blocks/tags/1.0.2/readme.txt

    r2338987 r2344005  
    55Requires at least: 5.4
    66Tested up to: 5.4
    7 Stable tag: 1.0.1
     7Stable tag: 1.0.2
    88Requires PHP: 5.6
    99License: GPLv2 or later
     
    4040== Changelog ==
    4141
     42= 1.0.2 =
     43* Fix Blog Card: Improved data acquisition accuracy
     44
    4245= 1.0.1 =
    4346* Fix Blog Card: Works without Open Graph
  • qroko-blocks/trunk/functions/open-graph.php

    r2338987 r2344005  
    11<?php
    2 /**
    3  * Copyright 2010 Scott MacVicar
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  * http://www.apache.org/licenses/LICENSE-2.0
    8  * Unless required by applicable law or agreed to in writing, software
    9  * distributed under the License is distributed on an "AS IS" BASIS,
    10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  * See the License for the specific language governing permissions and
    12  * limitations under the License.
    13  * Original can be found at https://github.com/scottmac/opengraph/blob/master/OpenGraph.php
    14  */
     2// Debug
     3//require __DIR__ . '/../debug/ChromePhp.php';
    154
    16 class OpenGraph implements Iterator {
    17   /**
    18    * There are base schema's based on type, this is just
    19    * a map so that the schema can be obtained
    20    *
    21    */
    22   public static $TYPES = array(
    23     'activity' => array('activity', 'sport'),
    24     'business' => array('bar', 'company', 'cafe', 'hotel', 'restaurant'),
    25     'group' => array('cause', 'sports_league', 'sports_team'),
    26     'organization' => array('band', 'government', 'non_profit', 'school', 'university'),
    27     'person' => array('actor', 'athlete', 'author', 'director', 'musician', 'politician', 'public_figure'),
    28     'place' => array('city', 'country', 'landmark', 'state_province'),
    29     'product' => array('album', 'book', 'drink', 'food', 'game', 'movie', 'product', 'song', 'tv_show'),
    30     'website' => array('blog', 'website'),
    31   );
    32 
    33   /**
    34    * Holds all the Open Graph values we've parsed from a page
    35    *
    36    */
    37   private $_values = array();
    38 
    39   /**
    40    * Fetches a URI and parses it for Open Graph data, returns
    41    * false on error.
    42    *
    43    * @param $URI    URI to page to parse for Open Graph data
    44    * @return OpenGraph
    45    */
    46   static public function fetch($URI) {
    47     $response = wp_remote_get($URI);
    48 
    49     if (!empty($response)) {
    50       return self::_parse($response['body']);
    51     } else {
    52       return false;
    53     }
    54   }
    55 
    56   /**
    57    * Parses HTML and extracts Open Graph data, this assumes
    58    * the document is at least well formed.
    59    *
    60    * @param $HTML    HTML to parse
    61    * @return OpenGraph
    62    */
    63   static private function _parse($HTML) {
    64     $old_libxml_error = libxml_use_internal_errors(true);
    65 
    66     $HTML = mb_convert_encoding($HTML,"HTML-ENTITIES","UTF-8");
    67 
    68     $doc = new DOMDocument();
    69     $doc->loadHTML($HTML);
    70 
    71     libxml_use_internal_errors($old_libxml_error);
    72 
    73     $tags = $doc->getElementsByTagName('meta');
    74     if (!$tags || $tags->length === 0) {
    75       return false;
    76     }
    77 
    78     $page = new self();
    79 
    80     $nonOgDescription = null;
    81 
    82     foreach ($tags AS $tag) {
    83       if ($tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) {
    84         $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
    85         $page->_values[$key] = $tag->getAttribute('content');
    86       }
    87 
    88       // Added this if loop to retrieve description values from sites like the New York Times who have malformed it.
    89       if ($tag ->hasAttribute('value') && $tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) {
    90         $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
    91         $page->_values[$key] = $tag->getAttribute('value');
    92       }
    93 
    94       // Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
    95       if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'description') {
    96         $nonOgDescription = $tag->getAttribute('content');
    97       }
    98     }
    99 
    100     // Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
    101     if (!isset($page->_values['title'])) {
    102       $titles = $doc->getElementsByTagName('title');
    103       if ($titles->length > 0) {
    104         $page->_values['title'] = $titles->item(0)->textContent;
    105       }
    106     }
    107 
    108     if (!isset($page->_values['description']) && $nonOgDescription) {
    109       $page->_values['description'] = $nonOgDescription;
    110     }
    111 
    112     // Fallback to use image_src if ogp::image isn't set.
    113     if (!isset($page->values['image'])) {
    114       $domxpath = new DOMXPath($doc);
    115       $elements = $domxpath->query("//link[@rel='image_src']");
    116 
    117       if ($elements->length > 0) {
    118         $domattr = $elements->item(0)->attributes->getNamedItem('href');
    119         if ($domattr) {
    120           $page->_values['image'] = $domattr->value;
    121           $page->_values['image_src'] = $domattr->value;
    122         }
    123       }
    124     }
    125 
    126     if (empty($page->_values)) { return false; }
    127 
    128     return $page;
    129   }
    130 
    131   /**
    132    * Helper method to access attributes directly
    133    * Example:
    134    * $graph->title
    135    *
    136    * @param $key    Key to fetch from the lookup
    137    */
    138   public function __get($key) {
    139     if (array_key_exists($key, $this->_values)) {
    140       return $this->_values[$key];
    141     }
    142 
    143     if ($key === 'schema') {
    144       foreach (self::$TYPES AS $schema => $types) {
    145         if (array_search($this->_values['type'], $types)) {
    146           return $schema;
    147         }
    148       }
    149     }
    150   }
    151 
    152   /**
    153    * Return all the keys found on the page
    154    *
    155    * @return array
    156    */
    157   public function keys() {
    158     return array_keys($this->_values);
    159   }
    160 
    161   /**
    162    * Helper method to check an attribute exists
    163    *
    164    * @param $key
    165    */
    166   public function __isset($key) {
    167     return array_key_exists($key, $this->_values);
    168   }
    169 
    170   /**
    171    * Will return true if the page has location data embedded
    172    *
    173    * @return boolean Check if the page has location data
    174    */
    175   public function hasLocation() {
    176     if (array_key_exists('latitude', $this->_values) && array_key_exists('longitude', $this->_values)) {
    177       return true;
    178     }
    179 
    180     $address_keys = array('street_address', 'locality', 'region', 'postal_code', 'country_name');
    181     $valid_address = true;
    182     foreach ($address_keys AS $key) {
    183       $valid_address = ($valid_address && array_key_exists($key, $this->_values));
    184     }
    185     return $valid_address;
    186   }
    187 
    188   /**
    189    * Iterator code
    190    */
    191   private $_position = 0;
    192   public function rewind() { reset($this->_values); $this->_position = 0; }
    193   public function current() { return current($this->_values); }
    194   public function key() { return key($this->_values); }
    195   public function next() { next($this->_values); ++$this->_position; }
    196   public function valid() { return $this->_position < sizeof($this->_values); }
    197 }
     5// Composer
     6require __DIR__ . '/../vendor/autoload.php';
     7use Embed\Embed;
    1988
    1999function open_graph() {
     
    20111  $esc_target_url = esc_url($target_url);
    20212
    203   $graph = OpenGraph::fetch($esc_target_url);
     13  $graph = Embed::create($esc_target_url);
    20414
    20515  $og_title = $graph->title;
     
    22939
    23040  // Debug: Chrome
    231   //include '../debug/ChromePhp.php';
     41  //ChromePhp::log(json_encode($graph));
    23242  //ChromePhp::log(json_encode($records));
    23343
  • qroko-blocks/trunk/qroko-blocks.php

    r2338987 r2344005  
    66 * Author: qrac
    77 * Author URI: https://qrac.jp/
    8  * Version: 1.0.1
     8 * Version: 1.0.2
    99 * Text Domain: qroko-blocks
    1010 * Domain Path: /languages
  • qroko-blocks/trunk/readme.txt

    r2338987 r2344005  
    55Requires at least: 5.4
    66Tested up to: 5.4
    7 Stable tag: 1.0.1
     7Stable tag: 1.0.2
    88Requires PHP: 5.6
    99License: GPLv2 or later
     
    4040== Changelog ==
    4141
     42= 1.0.2 =
     43* Fix Blog Card: Improved data acquisition accuracy
     44
    4245= 1.0.1 =
    4346* Fix Blog Card: Works without Open Graph
Note: See TracChangeset for help on using the changeset viewer.