PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/Avada/framework/plugins/multiple-featured-images/multiple-featured-images.php

https://gitlab.com/webkod3r/tripolis
PHP | 374 lines | 200 code | 67 blank | 107 comment | 24 complexity | 9d8cbefb79a352320ac63dfd1e952d6e MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Multiple Featured Images
  4. Description: Enables multiple featured images for posts and pages. If you like my plugin, feel free to give me reward ;) <a href="http://www.amazon.de/registry/wishlist/16KTW9ZG027C8" title="Amazon Wishlist" target="_blank">Amazon Wishlist</a>
  5. Version: 0.3
  6. Author: Marcus Kober
  7. Author URI: http://www.koeln-dialog.de/
  8. */
  9. /* Copyright 2012 Marcus Kober (m.kober@koeln-dialog.de)
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License, version 2, as
  12. published by the Free Software Foundation.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. if( !class_exists( 'kdMultipleFeaturedImages' ) ) {
  22. class kdMultipleFeaturedImages {
  23. private $id = '';
  24. private $post_type = '';
  25. private $labels = array();
  26. private $metabox_id = '';
  27. private $post_meta_key = '';
  28. private $nonce = '';
  29. private $default_labels = array(
  30. 'name' => 'Featured Image 2',
  31. 'set' => 'Set featured image 2',
  32. 'remove' => 'Remove featured image 2',
  33. 'use' => 'Use as featured image 2',
  34. );
  35. private $default_args = array(
  36. 'id' => 'featured-image-2',
  37. 'post_type' => 'page',
  38. );
  39. /**
  40. * Initialize the plugin
  41. *
  42. * @param array $args
  43. * @return void
  44. */
  45. public function __construct( $args ) {
  46. $this->labels = wp_parse_args( $args['labels'], $this->default_labels );
  47. unset( $args['labels'] );
  48. $args = wp_parse_args( $args, $this->default_args );
  49. $this->id = $args['id'];
  50. $this->post_type = $args['post_type'];
  51. $this->metabox_id = $this->id.'_'.$this->post_type;
  52. $this->post_meta_key= 'kd_'.$this->id.'_'.$this->post_type.'_id';
  53. $this->nonce = 'mfi-'.$this->id.$this->post_type;
  54. if( !current_theme_supports( 'post-thumbnails' ) ) {
  55. add_theme_support( 'post-thumbnails' );
  56. }
  57. add_action( 'admin_init', array( &$this, 'kd_admin_init' ) );
  58. add_action( 'add_meta_boxes', array( &$this, 'kd_add_meta_box' ) );
  59. add_filter( 'attachment_fields_to_edit', array( &$this, 'kd_add_attachment_field' ), 11, 2 );
  60. add_action( 'wp_ajax_set-MuFeaImg-'.$this->id.'-'.$this->post_type, array( &$this, 'kd_ajax_set_image' ) );
  61. add_action( 'delete_attachment', array( &$this, 'kd_delete_attachment' ) );
  62. }
  63. /**
  64. * Add admin-Javascript
  65. *
  66. * @return void
  67. */
  68. public function kd_admin_init() {
  69. if( strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/media-upload.php')) {
  70. wp_enqueue_script(
  71. 'kd-multiple-featured-images',
  72. get_template_directory_uri().'/framework/plugins/multiple-featured-images/js/kd-admin.js',
  73. 'jquery'
  74. );
  75. }
  76. }
  77. /**
  78. * Add admin metabox for choosing additional featured images
  79. *
  80. * @return void
  81. */
  82. public function kd_add_meta_box() {
  83. add_meta_box(
  84. $this->metabox_id,
  85. $this->labels['name'],
  86. array( $this, 'kd_meta_box_content' ),
  87. $this->post_type,
  88. 'side',
  89. 'low'
  90. );
  91. }
  92. /**
  93. * Output the metabox content
  94. *
  95. * @global object $post
  96. * @return void
  97. */
  98. public function kd_meta_box_content() {
  99. global $post;
  100. $image_id = get_post_meta(
  101. $post->ID,
  102. $this->post_meta_key,
  103. true
  104. );
  105. echo $this->kd_meta_box_output( $image_id );
  106. }
  107. /**
  108. * Generate the metabox content
  109. *
  110. * @global int $post_ID
  111. * @param int $image_id
  112. * @return string
  113. */
  114. public function kd_meta_box_output( $image_id = NULL ) {
  115. global $post_ID;
  116. $output = '';
  117. $setImageLink = sprintf(
  118. '<p class="hide-if-no-js"><a title="%2$s" href="%1$s" id="kd_%3$s" class="thickbox">%%s</a></p>',
  119. get_upload_iframe_src( 'image' ),
  120. $this->labels['set'],
  121. $this->id
  122. );
  123. if( $image_id && get_post( $image_id ) ) {
  124. $nonce_field = wp_create_nonce( $this->nonce.$post_ID );
  125. $thumbnail = wp_get_attachment_image( $image_id, array( 266, 266 ) );
  126. $output.= sprintf( $setImageLink, $thumbnail );
  127. $output.= '<p class="hide-if-no-js">';
  128. $output.= sprintf(
  129. '<a href="#" id="remove-%1$s-image" onclick="kdMuFeaImgRemove( \'%1$s\', \'%2$s\', \'%3$s\' ); return false;">',
  130. $this->id,
  131. $this->post_type,
  132. $nonce_field
  133. );
  134. $output.= $this->labels['remove'];
  135. $output.= '</a>';
  136. $output.= '</p>';
  137. return $output;
  138. }
  139. else {
  140. return sprintf( $setImageLink, $this->labels['set'] );
  141. }
  142. }
  143. /**
  144. * Create a new field in the image upload form
  145. *
  146. * @param string $form_fields
  147. * @param object $post
  148. * @return string
  149. */
  150. public function kd_add_attachment_field( $form_fields, $post ) {
  151. $calling_id = 0;
  152. if( isset( $_GET['post_id'] ) ) {
  153. $calling_id = absint( $_GET['post_id'] );
  154. }
  155. elseif( isset( $_POST ) && count( $_POST ) ) {
  156. $calling_id = $post->post_parent;
  157. }
  158. $calling_post = get_post( $calling_id );
  159. if( is_null( $calling_post ) || $calling_post->post_type != $this->post_type ) {
  160. return $form_fields;
  161. }
  162. $nonce_field = wp_create_nonce( $this->nonce.$calling_id );
  163. $output = sprintf(
  164. '<a href="#" id="%1$s-featuredimage" onclick="kdMuFeaImgSet( %3$s, \'%1$s\', \'%2$s\', \'%6$s\' ); return false;">%5$s</a>',
  165. $this->id,
  166. $this->post_type,
  167. $post->ID,
  168. $this->labels['name'],
  169. $this->labels['use'],
  170. $nonce_field
  171. );
  172. $form_fields['MuFeaImg-'.$this->id.'-'.$this->post_type] = array(
  173. 'label' => $this->labels['name'],
  174. 'input' => 'html',
  175. 'html' => $output
  176. );
  177. return $form_fields;
  178. }
  179. /**
  180. * Ajax function: set and delete featured image
  181. *
  182. * @global int $post_ID
  183. * @return void
  184. */
  185. public function kd_ajax_set_image() {
  186. global $post_ID;
  187. $post_ID = intval( $_POST['post_id'] );
  188. if( !current_user_can( 'edit_post', $post_ID ) ) {
  189. die( '-1' );
  190. }
  191. $thumb_id = intval( $_POST['thumbnail_id'] );
  192. if( $thumb_id == '-1' ) {
  193. delete_post_meta( $post_ID, $this->post_meta_key );
  194. die( $this->kd_meta_box_output( NULL ) );
  195. }
  196. if( $thumb_id && get_post( $thumb_id ) ) {
  197. $thumb_html = wp_get_attachment_image( $thumb_id, 'thumbnail' );
  198. if( !empty( $thumb_html ) ) {
  199. update_post_meta( $post_ID, $this->post_meta_key, $thumb_id );
  200. die( $this->kd_meta_box_output( $thumb_id ) );
  201. }
  202. }
  203. die( '0' );
  204. }
  205. /**
  206. * Delete custom featured image if attachmet is deleted
  207. *
  208. * @global object $wpdb
  209. * @param int $post_id
  210. * @return void
  211. */
  212. public function kd_delete_attachment( $post_id ) {
  213. global $wpdb;
  214. $wpdb->query(
  215. $wpdb->prepare(
  216. "DELETE FROM $wpdb->postmeta WHERE meta_key = '%s' AND meta_value = %d",
  217. $this->post_meta_key,
  218. $post_id
  219. )
  220. );
  221. }
  222. /**
  223. * Retrieve the id of the featured image
  224. *
  225. * @global object $post
  226. * @param string $image_id
  227. * @param string $post_type
  228. * @param int $post_id
  229. * @return int
  230. */
  231. public static function get_featured_image_id( $image_id, $post_type, $post_id = NULL) {
  232. global $post;
  233. if( is_null( $post_id ) ) {
  234. $post_id = get_the_ID();
  235. }
  236. return get_post_meta( $post_id, "kd_{$image_id}_{$post_type}_id", true);
  237. }
  238. /**
  239. * Return the featured image url
  240. *
  241. * @param string $image_id
  242. * @param string $post_type
  243. * @param int $post_id
  244. * @return string
  245. */
  246. public static function get_featured_image_url( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
  247. $id = self::get_featured_image_id( $image_id, $post_type, $post_id);
  248. if( $size != 'full' ) {
  249. $url = wp_get_attachment_image_src( $id, $size );
  250. $url = $url[0];
  251. }
  252. else {
  253. $url = wp_get_attachment_url( $id );
  254. }
  255. return $url;
  256. }
  257. /**
  258. * Return the featured image html output
  259. *
  260. * @param string $image_id
  261. * @param string $post_type
  262. * @param string $size
  263. * @param int $post_id
  264. * @return string
  265. */
  266. public static function get_the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
  267. $id = self::get_featured_image_id( $image_id, $post_type, $post_id);
  268. $output = '';
  269. if( $id ) {
  270. $output = wp_get_attachment_image(
  271. $id,
  272. $size,
  273. false
  274. );
  275. }
  276. return $output;
  277. }
  278. /**
  279. * Output the featured image html output
  280. *
  281. * @param string $image_id
  282. * @param string $post_type
  283. * @param string $size
  284. * @param int $post_id
  285. * @return void
  286. */
  287. public static function the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
  288. echo self::get_the_featured_image( $image_id, $post_type, $size, $post_id );
  289. }
  290. }
  291. }
  292. function kd_mfi_get_featured_image_id( $image_id, $post_type, $post_id = NULL ) {
  293. return kdMultipleFeaturedImages::get_featured_image_id( $image_id, $post_type, $post_id );
  294. }
  295. function kd_mfi_get_featured_image_url( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
  296. return kdMultipleFeaturedImages::get_featured_image_url( $image_id, $post_type, $size, $post_id );
  297. }
  298. function kd_mfi_get_the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
  299. return kdMultipleFeaturedImages::get_the_featured_image( $image_id, $post_type, $size, $post_id );
  300. }
  301. function kd_mfi_the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
  302. return kdMultipleFeaturedImages::the_featured_image( $image_id, $post_type, $size, $post_id );
  303. }
  304. // Omit closing PHP tag to avoid "Headers already sent" issues.