PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/includes/screen-meta-links.php

https://bitbucket.org/lgorence/quickpress
PHP | 270 lines | 160 code | 32 blank | 78 comment | 28 complexity | fb710019cbcf4beeff2306c9521a7499 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @author Janis Elsts
  4. * @copyright 2011
  5. */
  6. if ( !class_exists('wsScreenMetaLinks11') ):
  7. //Load JSON functions for PHP < 5.2
  8. if ( !(function_exists('json_encode') && function_exists('json_decode')) && !(class_exists('Services_JSON') || class_exists('Moxiecode_JSON')) ){
  9. $class_json_path = ABSPATH.WPINC.'/class-json.php';
  10. $class_moxiecode_json_path = ABSPATH.WPINC.'/js/tinymce/plugins/spellchecker/classes/utils/JSON.php';
  11. if ( file_exists($class_json_path) ){
  12. require $class_json_path;
  13. } elseif ( file_exists($class_moxiecode_json_path) ) {
  14. require $class_moxiecode_json_path;
  15. }
  16. }
  17. class wsScreenMetaLinks11 {
  18. var $registered_links; //List of meta links registered for each page.
  19. /**
  20. * Class constructor.
  21. *
  22. * @return void
  23. */
  24. function wsScreenMetaLinks11(){
  25. $this->registered_links = array();
  26. add_action('admin_notices', array(&$this, 'append_meta_links'));
  27. add_action('admin_print_styles', array(&$this, 'add_link_styles'));
  28. }
  29. /**
  30. * Add a new link to the screen meta area.
  31. *
  32. * Do not call this method directly. Instead, use the global add_screen_meta_link() function.
  33. *
  34. * @param string $id Link ID. Should be unique and a valid value for a HTML ID attribute.
  35. * @param string $text Link text.
  36. * @param string $href Link URL.
  37. * @param string|array $page The page(s) where you want to add the link.
  38. * @param array $attributes Optional. Additional attributes for the link tag.
  39. * @return void
  40. */
  41. function add_screen_meta_link($id, $text, $href, $page, $attributes = null){
  42. if ( !is_array($page) ){
  43. $page = array($page);
  44. }
  45. if ( is_null($attributes) ){
  46. $attributes = array();
  47. }
  48. //Basically a list of props for a jQuery() call
  49. $link = compact('id', 'text', 'href');
  50. $link = array_merge($link, $attributes);
  51. //Add the CSS classes that will make the look like a proper meta link
  52. if ( empty($link['class']) ){
  53. $link['class'] = '';
  54. }
  55. $link['class'] = 'show-settings custom-screen-meta-link ' . $link['class'];
  56. //Save the link in each relevant page's list
  57. foreach($page as $page_id){
  58. if ( !isset($this->registered_links[$page_id]) ){
  59. $this->registered_links[$page_id] = array();
  60. }
  61. $this->registered_links[$page_id][] = $link;
  62. }
  63. }
  64. /**
  65. * Output the JS that appends the custom meta links to the page.
  66. * Callback for the 'admin_notices' action.
  67. *
  68. * @access private
  69. * @return void
  70. */
  71. function append_meta_links(){
  72. global $hook_suffix;
  73. //Find links registered for this page
  74. $links = $this->get_links_for_page($hook_suffix);
  75. if ( empty($links) ){
  76. return;
  77. }
  78. ?>
  79. <script type="text/javascript">
  80. (function($, links){
  81. var container = $('#screen-meta-links');
  82. if ( container.length == 0 ) {
  83. container = $('<div />').attr('id', 'screen-meta-links').insertAfter('#screen-meta');
  84. }
  85. for(var i = 0; i < links.length; i++){
  86. container.append(
  87. $('<div/>')
  88. .attr({
  89. 'id' : links[i].id + '-wrap',
  90. 'class' : 'hide-if-no-js custom-screen-meta-link-wrap'
  91. })
  92. .append( $('<a/>', links[i]) )
  93. );
  94. }
  95. })(jQuery, <?php echo $this->json_encode($links); ?>);
  96. </script>
  97. <?php
  98. }
  99. /**
  100. * Get a list of custom screen meta links registered for a specific page.
  101. *
  102. * @param string $page
  103. * @return array
  104. */
  105. function get_links_for_page($page){
  106. $links = array();
  107. if ( isset($this->registered_links[$page]) ){
  108. $links = array_merge($links, $this->registered_links[$page]);
  109. }
  110. $page_as_screen = $this->page_to_screen_id($page);
  111. if ( ($page_as_screen != $page) && isset($this->registered_links[$page_as_screen]) ){
  112. $links = array_merge($links, $this->registered_links[$page_as_screen]);
  113. }
  114. return $links;
  115. }
  116. /**
  117. * Output the CSS code for custom screen meta links. Required because WP only
  118. * has styles for specific meta links (by #id), not meta links in general.
  119. *
  120. * Callback for 'admin_print_styles'.
  121. *
  122. * @access private
  123. * @return void
  124. */
  125. function add_link_styles(){
  126. global $hook_suffix;
  127. //Don't output the CSS if there are no custom meta links for this page.
  128. $links = $this->get_links_for_page($hook_suffix);
  129. if ( empty($links) ){
  130. return;
  131. }
  132. ?>
  133. <style type="text/css">
  134. .custom-screen-meta-link-wrap {
  135. float: right;
  136. height: 22px;
  137. padding: 0;
  138. margin: 0 0 0 6px;
  139. font-family: sans-serif;
  140. -moz-border-radius-bottomleft: 3px;
  141. -moz-border-radius-bottomright: 3px;
  142. -webkit-border-bottom-left-radius: 3px;
  143. -webkit-border-bottom-right-radius: 3px;
  144. border-bottom-left-radius: 3px;
  145. border-bottom-right-radius: 3px;
  146. background: #e3e3e3;
  147. border-right: 1px solid transparent;
  148. border-left: 1px solid transparent;
  149. border-bottom: 1px solid transparent;
  150. background-image: -ms-linear-gradient(bottom, #dfdfdf, #f1f1f1); /* IE10 */
  151. background-image: -moz-linear-gradient(bottom, #dfdfdf, #f1f1f1); /* Firefox */
  152. background-image: -o-linear-gradient(bottom, #dfdfdf, #f1f1f1); /* Opera */
  153. background-image: -webkit-gradient(linear, left bottom, left top, from(#dfdfdf), to(#f1f1f1)); /* old Webkit */
  154. background-image: -webkit-linear-gradient(bottom, #dfdfdf, #f1f1f1); /* new Webkit */
  155. background-image: linear-gradient(bottom, #dfdfdf, #f1f1f1); /* proposed W3C Markup */
  156. }
  157. #screen-meta .custom-screen-meta-link-wrap a.custom-screen-meta-link,
  158. #screen-meta-links .custom-screen-meta-link-wrap a.custom-screen-meta-link
  159. {
  160. background-image: none;
  161. padding-right: 6px;
  162. color: #777;
  163. }
  164. </style>
  165. <?php
  166. }
  167. /**
  168. * Convert a page hook name to a screen ID.
  169. *
  170. * @uses convert_to_screen()
  171. * @access private
  172. *
  173. * @param string $page
  174. * @return string
  175. */
  176. function page_to_screen_id($page){
  177. if ( function_exists('convert_to_screen') ){
  178. $screen = convert_to_screen($page);
  179. if ( isset($screen->id) ){
  180. return $screen->id;
  181. } else {
  182. return '';
  183. }
  184. } else {
  185. return str_replace( array('.php', '-new', '-add' ), '', $page);
  186. }
  187. }
  188. /**
  189. * Back-wards compatible json_encode(). Used to encode link data before
  190. * passing it to the JavaScript that actually creates the links.
  191. *
  192. * @param mixed $data
  193. * @return string
  194. */
  195. function json_encode($data){
  196. if ( function_exists('json_encode') ){
  197. return json_encode($data);
  198. }
  199. if ( class_exists('Services_JSON') ){
  200. $json = new Services_JSON();
  201. return( $json->encodeUnsafe($data) );
  202. } elseif ( class_exists('Moxiecode_JSON') ){
  203. $json = new Moxiecode_JSON();
  204. return $json->encode($data);
  205. } else {
  206. trigger_error('No JSON parser available', E_USER_ERROR);
  207. return null;
  208. }
  209. }
  210. }
  211. global $ws_screen_meta_links_versions;
  212. if ( !isset($ws_screen_meta_links_versions) ){
  213. $ws_screen_meta_links_versions = array();
  214. }
  215. $ws_screen_meta_links_versions['1.1'] = 'wsScreenMetaLinks11';
  216. endif;
  217. /**
  218. * Add a new link to the screen meta area.
  219. *
  220. * @param string $id Link ID. Should be unique and a valid value for a HTML ID attribute.
  221. * @param string $text Link text.
  222. * @param string $href Link URL.
  223. * @param string|array $page The page(s) where you want to add the link.
  224. * @param array $attributes Optional. Additional attributes for the link tag.
  225. * @return void
  226. */
  227. function add_screen_meta_link($id, $text, $href, $page, $attributes = null){
  228. global $ws_screen_meta_links_versions;
  229. static $instance = null;
  230. if ( is_null($instance) ){
  231. //Instantiate the latest version of the wsScreenMetaLinks class
  232. uksort($ws_screen_meta_links_versions, 'version_compare');
  233. $className = end($ws_screen_meta_links_versions);
  234. $instance = new $className;
  235. }
  236. $instance->add_screen_meta_link($id, $text, $href, $page, $attributes);
  237. }
  238. ?>