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

/wp-content/plugins/broken-link-checker/modules/parsers/metadata.php

https://bitbucket.org/lgorence/quickpress
PHP | 133 lines | 59 code | 20 blank | 54 comment | 7 complexity | f231bf779fadbc0710480eeceb6d4481 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: Metadata
  4. Description: Parses metadata (AKA custom fields)
  5. Version: 1.0
  6. Author: Janis Elsts
  7. ModuleID: metadata
  8. ModuleCategory: parser
  9. ModuleClassName: blcMetadataParser
  10. ModuleContext: on-demand
  11. ModuleLazyInit: true
  12. ModuleAlwaysActive: true
  13. ModuleHidden: true
  14. */
  15. class blcMetadataParser extends blcParser {
  16. var $supported_formats = array('metadata');
  17. var $supported_containers = array('custom_field');
  18. /**
  19. * Parse a metadata value.
  20. *
  21. * @param string|array $content Metadata value(s).
  22. * @param string $base_url The base URL to use for normalizing relative URLs. If ommitted, the blog's root URL will be used.
  23. * @param string $default_link_text
  24. * @return array An array of new blcLinkInstance objects.
  25. */
  26. function parse($content, $base_url = '', $default_link_text = ''){
  27. $instances = array();
  28. if ( !is_array($content) ){
  29. $content = array($content);
  30. }
  31. foreach($content as $value){
  32. //The complete contents of the meta field are stored in raw_url.
  33. //This is useful for editing/unlinking, when one may need to
  34. //distinguish between multiple fields with the same name.
  35. $raw_url = $value;
  36. //If this is a multiline metadata field take only the first line (workaround for the 'enclosure' field).
  37. $url = trim( array_shift( explode("\n", $value) ) );
  38. //Attempt to parse the URL
  39. $parts = @parse_url($url);
  40. if(!$parts) {
  41. return $instances; //Ignore invalid URLs
  42. };
  43. if ( !isset($parts['scheme']) ){
  44. //No sheme - likely a relative URL. Turn it into an absolute one.
  45. $url = $this->relative2absolute($url, $base_url);
  46. //Skip invalid URLs (again)
  47. if ( !$url || (strlen($url)<6) ) {
  48. return $instances;
  49. }
  50. }
  51. //The URL is okay, create and populate a new link instance.
  52. $instance = new blcLinkInstance();
  53. $instance->set_parser($this);
  54. $instance->raw_url = $raw_url;
  55. $instance->link_text = $default_link_text;
  56. $link_obj = new blcLink($url); //Creates or loads the link
  57. $instance->set_link($link_obj);
  58. $instances[] = $instance;
  59. }
  60. return $instances;
  61. }
  62. /**
  63. * Change the URL in a metadata field to another one.
  64. *
  65. * This is tricky because there can be multiple metadata fields with the same name
  66. * but different values. So we ignore $content (which might be an array of multiple
  67. * metadata values) and use the old raw_url that we stored when parsing the field(s)
  68. * instead.
  69. *
  70. * @see blcMetadataParser::parse()
  71. *
  72. * @param string $content Ignored.
  73. * @param string $new_url The new URL.
  74. * @param string $old_url Ignored.
  75. * @param string $old_raw_url The current meta value.
  76. *
  77. * @return array|WP_Error
  78. */
  79. function edit($content, $new_url, $old_url, $old_raw_url){
  80. //For multiline fields (like 'enclosure') we only want to change the first line.
  81. $lines = explode("\n", $old_raw_url);
  82. array_shift($lines); //Discard the old first line
  83. array_unshift($lines, $new_url); //Insert the new URL in its place.
  84. $content = implode("\n", $lines);
  85. return array(
  86. 'content' => $content,
  87. 'raw_url' => $new_url,
  88. );
  89. }
  90. /**
  91. * Get the link text for printing in the "Broken Links" table.
  92. *
  93. * @param blcLinkInstance $instance
  94. * @return string HTML
  95. */
  96. function ui_get_link_text($instance, $context = 'display'){
  97. $image_html = sprintf(
  98. '<img src="%s" class="blc-small-image" title="%2$s" alt="%2$s"> ',
  99. esc_attr( plugins_url('/images/script_code.png', BLC_PLUGIN_FILE) ),
  100. __('Custom field', 'broken-link-checker')
  101. );
  102. $field_html = sprintf(
  103. '<code>%s</code>',
  104. $instance->container_field
  105. );
  106. if ( $context != 'email' ){
  107. $field_html = $image_html . $field_html;
  108. }
  109. return $field_html;
  110. }
  111. }
  112. ?>