PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/modules/extras/embed-parser-base.php

https://bitbucket.org/lgorence/quickpress
PHP | 203 lines | 91 code | 30 blank | 82 comment | 9 complexity | c021a5604f71d7f4ca71795cf6610779 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @author Janis Elsts
  4. * @copyright 2010
  5. */
  6. if ( !class_exists('blcEmbedParserBase') ):
  7. /**
  8. * Base class for embedded video/audio parsers.
  9. *
  10. * Sub-classes should override the link_url_from_src() method and set the $url_search_string,
  11. * $short_title and $long_title properties to meaningful values.
  12. *
  13. * @package Broken Link Checker
  14. * @author Janis Elsts
  15. * @access public
  16. */
  17. class blcEmbedParserBase extends blcParser {
  18. var $supported_formats = array('html');
  19. var $short_title = ''; //Short desc. of embeds handled by this parser, singular. Example: "YouTube Video".
  20. var $long_title = ''; //Longer version of the above, e.g. "Embedded YouTube video".
  21. var $url_search_string = '';//Only consider embeds where the SRC contains this string. Example: "youtube.com/v/"
  22. /**
  23. * Parse a string for embed codes.
  24. *
  25. * @param string $content The text to parse.
  26. * @param string $base_url The base URL. Ignored.
  27. * @param string $default_link_text Default link text. Ignored.
  28. * @return array An array of new blcLinkInstance objects. The objects will include info about the embeds found, but not about the corresponding container entity.
  29. */
  30. function parse($content, $base_url = '', $default_link_text = ''){
  31. $instances = array();
  32. //Find likely-looking <embed> elements
  33. $embeds = $this->extract_embeds($content);
  34. foreach($embeds as $embed){
  35. //Do we know how to handle this embed? (first-pass verification)
  36. if ( strpos($embed['attributes']['src'], $this->url_search_string) === false ){
  37. continue;
  38. }
  39. //Get the original URL of the embedded object (may perform more complex verification)
  40. $url = $this->link_url_from_src($embed['attributes']['src']);
  41. if ( empty($url) ){
  42. continue;
  43. }
  44. //Create a new link instance.
  45. $instance = new blcLinkInstance();
  46. $instance->set_parser($this);
  47. $instance->raw_url = $embed['embed_code'];
  48. $instance->link_text = '[' . $this->short_title .']';
  49. $link_obj = new blcLink($url); //Creates or loads the link
  50. $instance->set_link($link_obj);
  51. $instances[] = $instance;
  52. }
  53. return $instances;
  54. }
  55. /**
  56. * Extract embedded elements from a HTML string.
  57. *
  58. * This function returns an array of <embed> elements found in the input
  59. * string. Embeds without a 'src' attribute are skipped.
  60. *
  61. * Each array item has the same basic structure as the array items
  62. * returned by blcUtility::extract_tags(), plus an additional 'embed_code' key
  63. * that contains the HTML code for the element. If the embed element is wrapped
  64. * in an <object>, the 'embed_code' key contains the full HTML for the entire
  65. * <object> + <embed> structure.
  66. *
  67. * @uses blcUtility::extract_tags() This function is a simple wrapper around extract_tags()
  68. *
  69. * @param string $html
  70. * @return array
  71. */
  72. function extract_embeds($html){
  73. $results = array();
  74. //remove all <code></code> blocks first
  75. $html = preg_replace('/<code[^>]*>.+?<\/code>/si', ' ', $html);
  76. //Find likely-looking <object> elements
  77. $objects = blcUtility::extract_tags($html, 'object', false, true);
  78. foreach($objects as $candidate){
  79. //Find the <embed> tag
  80. $embed = blcUtility::extract_tags($candidate['full_tag'], 'embed', false);
  81. if ( empty($embed)) continue;
  82. $embed = reset($embed); //Take the first (and only) found <embed> element
  83. if ( empty($embed['attributes']['src']) ){
  84. continue;
  85. }
  86. $embed['embed_code'] = $candidate['full_tag'];
  87. $results[] = $embed;
  88. //Remove the element so it doesn't come up when we search for plain <embed> elements.
  89. $html = str_replace($candidate['full_tag'], ' ', $html);
  90. }
  91. //Find <embed> elements not wrapped in an <object> element.
  92. $embeds = blcUtility::extract_tags($html, 'embed', false, true);
  93. foreach($embeds as $embed) {
  94. if ( !empty($embed['attributes']['src']) ){
  95. $embed['embed_code'] = $embed['full_tag'];
  96. $results[] = $embed;
  97. }
  98. }
  99. return $results;
  100. }
  101. /**
  102. * Remove all occurrences of the specified embed from a string.
  103. *
  104. * @param string $content Look for embeds in this string.
  105. * @param string $url Ignored.
  106. * @param string $embed_code The full embed code to look for.
  107. * @return string Input string with all matching embeds removed.
  108. */
  109. function unlink($content, $url, $embed_code){
  110. if ( empty($embed_code) ){
  111. return $content;
  112. }
  113. return str_replace($embed_code, '', $content); //Super-simple.
  114. }
  115. /**
  116. * Get the link text for printing in the "Broken Links" table.
  117. *
  118. * @param blcLinkInstance $instance
  119. * @param string $context
  120. * @return string HTML
  121. */
  122. function ui_get_link_text($instance, $context = 'display'){
  123. $image_url = sprintf(
  124. '/images/%s.png',
  125. $this->parser_type
  126. );
  127. $image_html = sprintf(
  128. '<img src="%s" class="blc-small-image" title="%2$s" alt="%2$s"> ',
  129. esc_attr( plugins_url($image_url, BLC_PLUGIN_FILE) ),
  130. $this->long_title
  131. );
  132. $field_html = sprintf(
  133. '%s',
  134. $this->short_title
  135. );
  136. if ( $context != 'email' ){
  137. $field_html = $image_html . $field_html;
  138. }
  139. return $field_html;
  140. }
  141. /**
  142. * Determine the original URL of an embedded object by analysing its SRC attribute.
  143. *
  144. * For example, if the object in question is an embedded YouTube video, this
  145. * method should return the URL of the original video; e.g. 'http://www.youtube.com/watch?v=example1234'
  146. *
  147. * Should be overridden in a sub-class.
  148. *
  149. * @return string The URL of the embedded object, or an empty string if the URL can't be determined.
  150. */
  151. function link_url_from_src(){
  152. return '';
  153. }
  154. /**
  155. * Editing is disabled in embed parsers. Calling this function will yield an instance of WP_Error.
  156. *
  157. * @param string $content
  158. * @param string $new_url
  159. * @param string $old_url
  160. * @param string $old_raw_url
  161. * @return WP_Error
  162. */
  163. function edit($content, $new_url, $old_url, $old_raw_url){
  164. return new WP_Error(
  165. 'not_implemented',
  166. sprintf(__("Embedded videos can't be edited using Broken Link Checker. Please edit or replace the video in question manually.", 'broken-link-checker'), $this->parser_type)
  167. );
  168. }
  169. }
  170. endif;
  171. ?>