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

/wp-content/plugins/cm-pop-up-banners/libs/wpalchemy/class/mediaaccess.php

https://gitlab.com/thisishayat/itv-2016
PHP | 414 lines | 175 code | 77 blank | 162 comment | 14 complexity | 45bcf2f58d281d5cbd37a59824fc66de MD5 | raw file
  1. <?php
  2. /**
  3. * @author Dimas Begunoff
  4. * @copyright Copyright (c) 2011, Dimas Begunoff, http://farinspace.com/
  5. * @license http://en.wikipedia.org/wiki/MIT_License The MIT License
  6. * @package WPAlchemy
  7. * @version 0.2.1
  8. * @link http://github.com/farinspace/wpalchemy/
  9. * @link http://farinspace.com/
  10. */
  11. class WPAlchemy_MediaAccess
  12. {
  13. /**
  14. * User defined identifier for the css class name of the HTML button element,
  15. * used when pairing the field and button elements
  16. *
  17. * @since 0.1
  18. * @access public
  19. * @var string required
  20. */
  21. public $button_class_name = 'mediabutton';
  22. /**
  23. * User defined identifier for the css class name of the HTML field element,
  24. * used when pairing the field and button elements
  25. *
  26. * @since 0.1
  27. * @access public
  28. * @var string required
  29. */
  30. public $field_class_name = 'mediafield';
  31. /**
  32. * User defined label for the insert button in the media upload box, this
  33. * can be set once or per field and button pair.
  34. *
  35. * @since 0.1
  36. * @access public
  37. * @var string optional
  38. * @see setInsertButtonLabel()
  39. */
  40. public $insert_button_label = null;
  41. /**
  42. * Used to track the current groupname for pairing a field and button.
  43. *
  44. * @since 0.1
  45. * @access private
  46. * @var string
  47. * @see setGroupName()
  48. */
  49. private $groupname = null;
  50. /**
  51. * Used to track the current tab for the media upload box.
  52. *
  53. * @since 0.1
  54. * @access private
  55. * @var string
  56. * @see setTab()
  57. */
  58. private $tab = null;
  59. /**
  60. * MediaAccess class
  61. *
  62. * @since 0.1
  63. * @access public
  64. * @param array $a
  65. */
  66. public function __construct(array $a = array())
  67. {
  68. foreach ($a as $n => $v)
  69. {
  70. $this->$n = $v;
  71. }
  72. if ( ! defined('WPALCHEMY_SEND_TO_EDITOR_ENABLED'))
  73. {
  74. add_action('admin_footer', array($this, 'init'));
  75. define('WPALCHEMY_SEND_TO_EDITOR_ENABLED', true);
  76. }
  77. }
  78. /**
  79. * Used to generate short unique/random names
  80. *
  81. * @since 0.1
  82. * @access public
  83. * @return string
  84. */
  85. private function getName()
  86. {
  87. return substr(md5(microtime() . rand()), rand(0,25), 6);
  88. }
  89. /**
  90. * Used to set the insert button label in the media upload box, this can be
  91. * set once or per field and button pair.
  92. *
  93. * @since 0.1
  94. * @access public
  95. * @param string $label button label/title
  96. * @return object $this
  97. * @see setGroupName()
  98. */
  99. public function setInsertButtonLabel($label = 'Insert')
  100. {
  101. $this->insert_button_label = $label;
  102. return $this;
  103. }
  104. public function setTab($name)
  105. {
  106. $this->tab = $name;
  107. $this;
  108. }
  109. /**
  110. * Used before calls to getField(), getButton() or getButtonClass() to set
  111. * the groupname to pair a field and button element.
  112. *
  113. * @since 0.1
  114. * @access public
  115. * @param string $name unique name per pair of field and button
  116. * @return object $this
  117. * @see setInsertButtonLabel()
  118. */
  119. public function setGroupName($name)
  120. {
  121. $this->groupname = $name;
  122. return $this;
  123. }
  124. /**
  125. * Used to insert a form field of type "text", this should be paired with a
  126. * button element. The name and value attributes are required.
  127. *
  128. * @since 0.1
  129. * @access public
  130. * @param array $attr INPUT tag parameters
  131. * @return HTML
  132. * @see getButton()
  133. */
  134. public function getField(array $attr)
  135. {
  136. $groupname = isset($attr['groupname']) ? $attr['groupname'] : $this->groupname ;
  137. $attr_default = array
  138. (
  139. 'type' => 'text',
  140. 'class' => $this->field_class_name . '-' . $groupname,
  141. );
  142. ###
  143. if (isset($attr['class']))
  144. {
  145. $attr['class'] = $attr_default['class'] . ' ' . trim($attr['class']);
  146. }
  147. $attr = array_merge($attr_default, $attr);
  148. ###
  149. $elem_attr = array();
  150. foreach ($attr as $n => $v)
  151. {
  152. array_push($elem_attr, $n . '="' . $v . '"');
  153. }
  154. ###
  155. return '<input ' . implode(' ', $elem_attr) . '/>';
  156. }
  157. /**
  158. * Used to get the link used for the button element. If creating custom
  159. * buttons, this method should be used to get the link needed for proper
  160. * functionality.
  161. *
  162. * @since 0.1
  163. * @access public
  164. * @param string $tab name that the media upload box will initially load
  165. * @return string link
  166. * @see getButtonClass(), getButton()
  167. */
  168. public function getButtonLink($tab = null)
  169. {
  170. // this is set even for new posts/pages
  171. global $post_ID; //wp
  172. $tab = ! empty($tab) ? $tab : $this->tab ;
  173. $tab = ! empty($tab) ? $tab : 'library' ;
  174. return 'media-upload.php?post_id=' . $post_ID . '&tab=' . $tab . '&TB_iframe=1';
  175. }
  176. /**
  177. * Used to get the CSS class name(s) used for the button element. If
  178. * creating custom buttons, this method should be used to get the css class
  179. * names needed for proper functionality.
  180. *
  181. * @since 0.1
  182. * @access public
  183. * @param string $groupname name used when pairing a text field and button
  184. * @return string css class(es)
  185. * @see getButtonLink(), getButton()
  186. */
  187. public function getButtonClass($groupname = null)
  188. {
  189. $groupname = isset($groupname) ? $groupname : $this->groupname ;
  190. return $this->button_class_name . '-' . $groupname . ' thickbox';
  191. }
  192. /**
  193. * Used to get the CSS class name used for the field element. If
  194. * creating a custom field, this method should be used to get the css class
  195. * name needed for proper functionality.
  196. *
  197. * @since 0.2
  198. * @access public
  199. * @param string $groupname name used when pairing a text field and button
  200. * @return string css class(es)
  201. * @see getButtonClass(), getField()
  202. */
  203. public function getFieldClass($groupname = null)
  204. {
  205. $groupname = isset($groupname) ? $groupname : $this->groupname ;
  206. return $this->field_class_name . '-' . $groupname;
  207. }
  208. /**
  209. * Used to insert a WordPress styled button, should be paired with a text
  210. * field element.
  211. *
  212. * @since 0.1
  213. * @access public
  214. * @return HTML
  215. * @see getField(), getButtonClass(), getButtonLink()
  216. */
  217. public function getButton(array $attr = array())
  218. {
  219. $groupname = isset($attr['groupname']) ? $attr['groupname'] : $this->groupname ;
  220. $tab = isset($attr['tab']) ? $attr['tab'] : $this->tab ;
  221. $attr_default = array
  222. (
  223. 'label' => 'Add Media',
  224. 'href' => $this->getButtonLink($tab),
  225. 'class' => $this->getButtonClass($groupname) . ' button',
  226. );
  227. if (isset($this->insert_button_label))
  228. {
  229. $attr_default['class'] .= " {label:'" . $this->insert_button_label . "'}";
  230. }
  231. ###
  232. if (isset($attr['class']))
  233. {
  234. $attr['class'] = $attr_default['class'] . ' ' . trim($attr['class']);
  235. }
  236. $attr = array_merge($attr_default, $attr);
  237. $label = $attr['label'];
  238. unset($attr['label']);
  239. ###
  240. $elem_attr = array();
  241. foreach ($attr as $n => $v)
  242. {
  243. array_push($elem_attr, $n . '="' . $v . '"');
  244. }
  245. ###
  246. return '<a ' . implode(' ', $elem_attr) . '>' . $label . '</a>';
  247. }
  248. /**
  249. * Used to insert global STYLE or SCRIPT tags into the footer, called on
  250. * WordPress admin_footer action.
  251. *
  252. * @since 0.1
  253. * @access public
  254. * @return HTML/Javascript
  255. */
  256. public function init()
  257. {
  258. $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL ;
  259. $file = basename(parse_url($uri, PHP_URL_PATH));
  260. if ($uri AND in_array($file, array('post.php', 'post-new.php')))
  261. {
  262. // include javascript for special functionality
  263. ?><script type="text/javascript">
  264. /* <![CDATA[ */
  265. var interval = null;
  266. jQuery(function($)
  267. {
  268. if (typeof send_to_editor === 'function')
  269. {
  270. var wpalchemy_insert_button_label = '';
  271. var wpalchemy_mediafield = null;
  272. var wpalchemy_send_to_editor_default = send_to_editor;
  273. send_to_editor = function(html)
  274. {
  275. clearInterval(interval);
  276. if (wpalchemy_mediafield)
  277. {
  278. var src = html.match(/src=['|"](.*?)['|"] alt=/i);
  279. src = (src && src[1]) ? src[1] : '' ;
  280. var href = html.match(/href=['|"](.*?)['|"]/i);
  281. href = (href && href[1]) ? href[1] : '' ;
  282. var url = src ? src : href ;
  283. wpalchemy_mediafield.val(url);
  284. // reset insert button label
  285. setInsertButtonLabel(wpalchemy_insert_button_label);
  286. wpalchemy_mediafield = null;
  287. }
  288. else
  289. {
  290. wpalchemy_send_to_editor_default(html);
  291. }
  292. tb_remove();
  293. }
  294. function getInsertButtonLabel()
  295. {
  296. return $('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').val();
  297. }
  298. function setInsertButtonLabel(label)
  299. {
  300. $('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').val(label);
  301. }
  302. $('[class*="<?php echo $this->button_class_name; ?>"]').on('click', function()
  303. {
  304. var name = $(this).attr('class').match(/<?php echo $this->button_class_name; ?>-([a-zA-Z0-9_-]*)/i);
  305. name = (name && name[1]) ? name[1] : '' ;
  306. var data = $(this).attr('class').match(/({.*})/i);
  307. data = (data && data[1]) ? data[1] : '' ;
  308. data = eval("(" + (data.indexOf('{') < 0 ? '{' + data + '}' : data) + ")");
  309. wpalchemy_mediafield = $('.<?php echo $this->field_class_name; ?>-' + name, $(this).closest('.postbox'));
  310. function iframeSetup()
  311. {
  312. if ($('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').length)
  313. {
  314. // run once
  315. if ( ! wpalchemy_insert_button_label.length)
  316. {
  317. wpalchemy_insert_button_label = getInsertButtonLabel();
  318. }
  319. setInsertButtonLabel((data && data.label)?data.label:'Insert');
  320. // tab "type" needs a timer in order to properly change the button label
  321. //clearInterval(interval);
  322. // setup iframe.load as soon as it becomes available
  323. // prevent multiple binds
  324. //$('#TB_iframeContent').unbind('load', iframeSetup).bind('load', iframeSetup);
  325. }
  326. }
  327. clearInterval(interval);
  328. interval = setInterval(iframeSetup, 500);
  329. });
  330. }
  331. });
  332. /* ]]> */
  333. </script><?php
  334. }
  335. }
  336. }
  337. /* End of file */