PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/image-widget/image-widget.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 406 lines | 258 code | 47 blank | 101 comment | 52 complexity | adece3633c68e0636ada1f2ddf0c350b MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Image Widget
  4. Plugin URI: http://wordpress.org/extend/plugins/image-widget/
  5. Description: A simple image widget that uses the native WordPress media manager to add image widgets to your site.
  6. Author: Modern Tribe, Inc.
  7. Version: 4.0.7
  8. Author URI: http://tri.be
  9. */
  10. // Block direct requests
  11. if ( !defined('ABSPATH') )
  12. die('-1');
  13. // Load the widget on widgets_init
  14. function tribe_load_image_widget() {
  15. register_widget('Tribe_Image_Widget');
  16. }
  17. add_action('widgets_init', 'tribe_load_image_widget');
  18. /**
  19. * Tribe_Image_Widget class
  20. **/
  21. class Tribe_Image_Widget extends WP_Widget {
  22. const VERSION = '4.0.6';
  23. const CUSTOM_IMAGE_SIZE_SLUG = 'tribe_image_widget_custom';
  24. /**
  25. * Tribe Image Widget constructor
  26. *
  27. * @author Modern Tribe, Inc.
  28. */
  29. function Tribe_Image_Widget() {
  30. load_plugin_textdomain( 'image_widget', false, trailingslashit(basename(dirname(__FILE__))) . 'lang/');
  31. $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'image_widget' ) );
  32. $control_ops = array( 'id_base' => 'widget_sp_image' );
  33. $this->WP_Widget('widget_sp_image', __('Image Widget', 'image_widget'), $widget_ops, $control_ops);
  34. if ( $this->use_old_uploader() ) {
  35. require_once( 'lib/ImageWidgetDeprecated.php' );
  36. new ImageWidgetDeprecated( $this );
  37. } else {
  38. add_action( 'sidebar_admin_setup', array( $this, 'admin_setup' ) );
  39. }
  40. add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
  41. add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ),10 ,2 );
  42. if ( !defined('I_HAVE_SUPPORTED_THE_IMAGE_WIDGET') )
  43. add_action( 'admin_notices', array( $this, 'post_upgrade_nag') );
  44. add_action( 'network_admin_notices', array( $this, 'post_upgrade_nag') );
  45. }
  46. /**
  47. * Test to see if this version of WordPress supports the new image manager.
  48. * @return bool true if the current version of WordPress does NOT support the current image management tech.
  49. */
  50. private function use_old_uploader() {
  51. if ( defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) return true;
  52. return !function_exists('wp_enqueue_media');
  53. }
  54. /**
  55. * Enqueue all the javascript.
  56. */
  57. function admin_setup() {
  58. wp_enqueue_media();
  59. wp_enqueue_script( 'tribe-image-widget', plugins_url('resources/js/image-widget.js', __FILE__), array( 'jquery', 'media-upload', 'media-views' ), self::VERSION );
  60. wp_localize_script( 'tribe-image-widget', 'TribeImageWidget', array(
  61. 'frame_title' => __( 'Select an Image', 'image_widget' ),
  62. 'button_title' => __( 'Insert Into Widget', 'image_widget' ),
  63. ) );
  64. }
  65. /**
  66. * Widget frontend output
  67. *
  68. * @param array $args
  69. * @param array $instance
  70. * @author Modern Tribe, Inc.
  71. */
  72. function widget( $args, $instance ) {
  73. extract( $args );
  74. $instance = wp_parse_args( (array) $instance, self::get_defaults() );
  75. if ( !empty( $instance['imageurl'] ) || !empty( $instance['attachment_id'] ) ) {
  76. $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'] );
  77. $instance['description'] = apply_filters( 'widget_text', $instance['description'], $args, $instance );
  78. $instance['link'] = apply_filters( 'image_widget_image_link', esc_url( $instance['link'] ), $args, $instance );
  79. $instance['linktarget'] = apply_filters( 'image_widget_image_link_target', esc_attr( $instance['linktarget'] ), $args, $instance );
  80. $instance['width'] = apply_filters( 'image_widget_image_width', abs( $instance['width'] ), $args, $instance );
  81. $instance['height'] = apply_filters( 'image_widget_image_height', abs( $instance['height'] ), $args, $instance );
  82. $instance['align'] = apply_filters( 'image_widget_image_align', esc_attr( $instance['align'] ), $args, $instance );
  83. $instance['alt'] = apply_filters( 'image_widget_image_alt', esc_attr( $instance['alt'] ), $args, $instance );
  84. if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
  85. $instance['attachment_id'] = ( $instance['attachment_id'] > 0 ) ? $instance['attachment_id'] : $instance['image'];
  86. $instance['attachment_id'] = apply_filters( 'image_widget_image_attachment_id', abs( $instance['attachment_id'] ), $args, $instance );
  87. $instance['size'] = apply_filters( 'image_widget_image_size', esc_attr( $instance['size'] ), $args, $instance );
  88. }
  89. $instance['imageurl'] = apply_filters( 'image_widget_image_url', esc_url( $instance['imageurl'] ), $args, $instance );
  90. // No longer using extracted vars. This is here for backwards compatibility.
  91. extract( $instance );
  92. include( $this->getTemplateHierarchy( 'widget' ) );
  93. }
  94. }
  95. /**
  96. * Update widget options
  97. *
  98. * @param object $new_instance Widget Instance
  99. * @param object $old_instance Widget Instance
  100. * @return object
  101. * @author Modern Tribe, Inc.
  102. */
  103. function update( $new_instance, $old_instance ) {
  104. $instance = $old_instance;
  105. $new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
  106. $instance['title'] = strip_tags($new_instance['title']);
  107. if ( current_user_can('unfiltered_html') ) {
  108. $instance['description'] = $new_instance['description'];
  109. } else {
  110. $instance['description'] = wp_filter_post_kses($new_instance['description']);
  111. }
  112. $instance['link'] = $new_instance['link'];
  113. $instance['linktarget'] = $new_instance['linktarget'];
  114. $instance['width'] = abs( $new_instance['width'] );
  115. $instance['height'] =abs( $new_instance['height'] );
  116. if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
  117. $instance['size'] = $new_instance['size'];
  118. }
  119. $instance['align'] = $new_instance['align'];
  120. $instance['alt'] = $new_instance['alt'];
  121. // Reverse compatibility with $image, now called $attachement_id
  122. if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) && $new_instance['attachment_id'] > 0 ) {
  123. $instance['attachment_id'] = abs( $new_instance['attachment_id'] );
  124. } elseif ( $new_instance['image'] > 0 ) {
  125. $instance['attachment_id'] = $instance['image'] = abs( $new_instance['image'] );
  126. if ( class_exists('ImageWidgetDeprecated') ) {
  127. $instance['imageurl'] = ImageWidgetDeprecated::get_image_url( $instance['image'], $instance['width'], $instance['height'] ); // image resizing not working right now
  128. }
  129. }
  130. $instance['imageurl'] = $new_instance['imageurl']; // deprecated
  131. $instance['aspect_ratio'] = $this->get_image_aspect_ratio( $instance );
  132. return $instance;
  133. }
  134. /**
  135. * Form UI
  136. *
  137. * @param object $instance Widget Instance
  138. * @author Modern Tribe, Inc.
  139. */
  140. function form( $instance ) {
  141. $instance = wp_parse_args( (array) $instance, self::get_defaults() );
  142. if ( $this->use_old_uploader() ) {
  143. include( $this->getTemplateHierarchy( 'widget-admin.deprecated' ) );
  144. } else {
  145. include( $this->getTemplateHierarchy( 'widget-admin' ) );
  146. }
  147. }
  148. /**
  149. * Admin header css
  150. *
  151. * @author Modern Tribe, Inc.
  152. */
  153. function admin_head() {
  154. ?>
  155. <style type="text/css">
  156. .uploader input.button {
  157. width: 100%;
  158. height: 34px;
  159. line-height: 33px;
  160. }
  161. .tribe_preview .aligncenter {
  162. display: block;
  163. margin-left: auto !important;
  164. margin-right: auto !important;
  165. }
  166. .tribe_preview {
  167. overflow: hidden;
  168. max-height: 300px;
  169. }
  170. .tribe_preview img {
  171. margin: 10px 0;
  172. }
  173. </style>
  174. <?php
  175. }
  176. /**
  177. * Render an array of default values.
  178. *
  179. * @return array default values
  180. */
  181. private static function get_defaults() {
  182. $defaults = array(
  183. 'title' => '',
  184. 'description' => '',
  185. 'link' => '',
  186. 'linktarget' => '',
  187. 'width' => 0,
  188. 'height' => 0,
  189. 'image' => 0, // reverse compatible - now attachement_id
  190. 'imageurl' => '', // reverse compatible.
  191. 'align' => 'none',
  192. 'alt' => '',
  193. );
  194. if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
  195. $defaults['size'] = self::CUSTOM_IMAGE_SIZE_SLUG;
  196. $defaults['attachment_id'] = 0;
  197. }
  198. return $defaults;
  199. }
  200. /**
  201. * Render the image html output.
  202. *
  203. * @param array $instance
  204. * @param bool $include_link will only render the link if this is set to true. Otherwise link is ignored.
  205. * @return string image html
  206. */
  207. private function get_image_html( $instance, $include_link = true ) {
  208. // Backwards compatible image display.
  209. if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) {
  210. $instance['attachment_id'] = $instance['image'];
  211. }
  212. $output = '';
  213. if ( $include_link && !empty( $instance['link'] ) ) {
  214. $attr = array(
  215. 'href' => $instance['link'],
  216. 'target' => $instance['linktarget'],
  217. 'class' => $this->widget_options['classname'].'-image-link',
  218. 'title' => ( !empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'],
  219. );
  220. $attr = apply_filters('image_widget_link_attributes', $attr, $instance );
  221. $attr = array_map( 'esc_attr', $attr );
  222. $output = '<a';
  223. foreach ( $attr as $name => $value ) {
  224. $output .= sprintf( ' %s="%s"', $name, $value );
  225. }
  226. $output .= '>';
  227. }
  228. $size = $this->get_image_size( $instance );
  229. if ( is_array( $size ) ) {
  230. $instance['width'] = $size[0];
  231. $instance['height'] = $size[1];
  232. } elseif ( !empty( $instance['attachment_id'] ) ) {
  233. //$instance['width'] = $instance['height'] = 0;
  234. $image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size );
  235. if ($image_details) {
  236. $instance['imageurl'] = $image_details[0];
  237. $instance['width'] = $image_details[1];
  238. $instance['height'] = $image_details[2];
  239. }
  240. }
  241. $instance['width'] = abs( $instance['width'] );
  242. $instance['height'] = abs( $instance['height'] );
  243. $attr = array();
  244. $attr['alt'] = $instance['title'];
  245. if (is_array($size)) {
  246. $attr['class'] = 'attachment-'.join('x',$size);
  247. } else {
  248. $attr['class'] = 'attachment-'.$size;
  249. }
  250. $attr['style'] = '';
  251. if (!empty($instance['width'])) {
  252. $attr['style'] .= "max-width: {$instance['width']}px;";
  253. }
  254. if (!empty($instance['height'])) {
  255. $attr['style'] .= "max-height: {$instance['height']}px;";
  256. }
  257. if (!empty($instance['align']) && $instance['align'] != 'none') {
  258. $attr['class'] .= " align{$instance['align']}";
  259. }
  260. $attr = apply_filters( 'image_widget_image_attributes', $attr, $instance );
  261. // If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids.
  262. if ( !empty( $instance['imageurl'] ) ) {
  263. // If all we have is an image src url we can still render an image.
  264. $attr['src'] = $instance['imageurl'];
  265. $attr = array_map( 'esc_attr', $attr );
  266. $hwstring = image_hwstring( $instance['width'], $instance['height'] );
  267. $output .= rtrim("<img $hwstring");
  268. foreach ( $attr as $name => $value ) {
  269. $output .= sprintf( ' %s="%s"', $name, $value );
  270. }
  271. $output .= ' />';
  272. } elseif( abs( $instance['attachment_id'] ) > 0 ) {
  273. $output .= wp_get_attachment_image($instance['attachment_id'], $size, false, $attr);
  274. }
  275. if ( $include_link && !empty( $instance['link'] ) ) {
  276. $output .= '</a>';
  277. }
  278. return $output;
  279. }
  280. /**
  281. * Assesses the image size in case it has not been set or in case there is a mismatch.
  282. *
  283. * @param $instance
  284. * @return array|string
  285. */
  286. private function get_image_size( $instance ) {
  287. if ( !empty( $instance['size'] ) && $instance['size'] != self::CUSTOM_IMAGE_SIZE_SLUG ) {
  288. $size = $instance['size'];
  289. } elseif ( isset( $instance['width'] ) && is_numeric($instance['width']) && isset( $instance['height'] ) && is_numeric($instance['height']) ) {
  290. $size = array(abs($instance['width']),abs($instance['height']));
  291. } else {
  292. $size = 'full';
  293. }
  294. return $size;
  295. }
  296. /**
  297. * Establish the aspect ratio of the image.
  298. *
  299. * @param $instance
  300. * @return float|number
  301. */
  302. private function get_image_aspect_ratio( $instance ) {
  303. if ( !empty( $instance['aspect_ratio'] ) ) {
  304. return abs( $instance['aspect_ratio'] );
  305. } else {
  306. $attachment_id = ( !empty($instance['attachment_id']) ) ? $instance['attachment_id'] : $instance['image'];
  307. if ( !empty($attachment_id) ) {
  308. $image_details = wp_get_attachment_image_src( $attachment_id, 'full' );
  309. if ($image_details) {
  310. return ( $image_details[1]/$image_details[2] );
  311. }
  312. }
  313. }
  314. }
  315. /**
  316. * Loads theme files in appropriate hierarchy: 1) child theme,
  317. * 2) parent template, 3) plugin resources. will look in the image-widget/
  318. * directory in a theme and the views/ directory in the plugin
  319. *
  320. * @param string $template template file to search for
  321. * @return template path
  322. * @author Modern Tribe, Inc. (Matt Wiebe)
  323. **/
  324. function getTemplateHierarchy($template) {
  325. // whether or not .php was added
  326. $template_slug = rtrim($template, '.php');
  327. $template = $template_slug . '.php';
  328. if ( $theme_file = locate_template(array('image-widget/'.$template)) ) {
  329. $file = $theme_file;
  330. } else {
  331. $file = 'views/' . $template;
  332. }
  333. return apply_filters( 'sp_template_image-widget_'.$template, $file);
  334. }
  335. /**
  336. * Display a thank you nag when the plugin has been upgraded.
  337. */
  338. public function post_upgrade_nag() {
  339. if ( !current_user_can('install_plugins') ) return;
  340. $version_key = '_image_widget_version';
  341. if ( get_site_option( $version_key ) == self::VERSION ) return;
  342. $msg = sprintf(__('Thanks for upgrading the Image Widget! If you like this plugin, please consider <a href="%s" target="_blank">rating it</a> and maybe even check out our premium plugins including our <a href="%s" target="_blank">Events Calendar Pro</a>!', 'image-widget'),'http://wordpress.org/extend/plugins/image-widget/?source=image-widget&pos=nag','http://tri.be/wordpress-events-calendar-pro/?source=image-widget&pos=nag');
  343. echo "<div class='update-nag'>$msg</div>";
  344. update_site_option( $version_key, self::VERSION );
  345. }
  346. /**
  347. * Display an informational section in the plugin admin ui.
  348. * @param $meta
  349. * @param $file
  350. *
  351. * @return array
  352. */
  353. public function plugin_row_meta( $meta, $file ) {
  354. if ( $file == plugin_basename( dirname(__FILE__).'/image-widget.php' ) ) {
  355. $meta[] = '<span class="tribe-test">'.sprintf(__('Check out our other <a href="%s" target="_blank">plugins</a> including our <a href="%s" target="_blank">Events Calendar Pro</a>!', 'image-widget'),'http://tri.be/products/?source=image-widget&pos=pluginlist','http://tri.be/wordpress-events-calendar-pro/?source=image-widget&pos=pluginlist').'</span>';
  356. }
  357. return $meta;
  358. }
  359. }