|
1 <?php |
|
2 /** |
|
3 * Class SlideshowPluginShortcode provides the shortcode function, which is called |
|
4 * on use of shortcode anywhere in the posts and pages. Also provides the shortcode |
|
5 * inserter, so that it's made easier for non-programmers to insert the shortcode |
|
6 * into a post or page. |
|
7 * |
|
8 * @since 1.2.0 |
|
9 * @author: Stefan Boonstra |
|
10 * @version: 25-09-12 |
|
11 */ |
|
12 class SlideshowPluginShortcode { |
|
13 |
|
14 /** Variables */ |
|
15 public static $shortCode = 'slideshow_deploy'; |
|
16 public static $bookmark = '!slideshow_deploy!'; |
|
17 private static $postIds = array(); |
|
18 |
|
19 /** |
|
20 * Initializes the shortcode, registering it and hooking the shortcode |
|
21 * inserter media buttons. |
|
22 * |
|
23 * @since 2.1.16 |
|
24 */ |
|
25 static function init(){ |
|
26 // Register shortcode |
|
27 add_shortcode(self::$shortCode, array(__CLASS__, 'slideshowDeploy')); |
|
28 |
|
29 // Admin |
|
30 if(is_admin()){ |
|
31 // Add shortcode inserter HTML |
|
32 add_action('media_buttons', array(__CLASS__, 'shortcodeInserter'), 11); |
|
33 |
|
34 // Enqueue shortcode inserter script |
|
35 add_action('admin_enqueue_scripts', array(__CLASS__, 'shortcodeInserterScript')); |
|
36 } |
|
37 } |
|
38 |
|
39 /** |
|
40 * Function slideshowDeploy adds a bookmark to where ever a shortcode |
|
41 * is found and adds the postId to an array, it then is loaded after |
|
42 * Wordpress has done its HTML checks. |
|
43 * |
|
44 * @since 1.2.0 |
|
45 * @param mixed $atts |
|
46 * @return String $output |
|
47 */ |
|
48 static function slideshowDeploy($atts){ |
|
49 $postId = ''; |
|
50 if(isset($atts['id'])) |
|
51 $postId = $atts['id']; |
|
52 |
|
53 $output = ''; |
|
54 $settings = SlideshowPluginPostType::getSimpleSettings($postId, null, false); |
|
55 if($settings['setting_avoidFilter'] == 'true'){ |
|
56 // Filter content after all Wordpress HTML parsers are done, then replace bookmarks with raw HTML |
|
57 add_filter('the_content', array(__CLASS__, 'insertSlideshow'), 999); |
|
58 add_filter('the_excerpt', array(__CLASS__, 'insertSlideshow'), 999); |
|
59 |
|
60 // Save post id |
|
61 self::$postIds[] = $postId; |
|
62 |
|
63 // Set output |
|
64 $output = self::$bookmark; |
|
65 }else{ |
|
66 // Just output the slideshow, without filtering |
|
67 $output = SlideshowPlugin::prepare($postId); |
|
68 } |
|
69 |
|
70 // Return output |
|
71 return $output; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Function insertSlideshow uses the prepare method of class SlideshowPlugin |
|
76 * to insert the code for the slideshow on the location a bookmark was found. |
|
77 * |
|
78 * @since 2.1.8 |
|
79 * @param String $content |
|
80 * @return String $content |
|
81 */ |
|
82 static function insertSlideshow($content){ |
|
83 // Loop through post ids |
|
84 if(is_array(self::$postIds) && count(self::$postIds) > 0) |
|
85 foreach(self::$postIds as $postId){ |
|
86 $updatedContent = preg_replace("/" . self::$bookmark . "/", SlideshowPlugin::prepare($postId), $content, 1); |
|
87 |
|
88 if(is_string($updatedContent)) |
|
89 $content = $updatedContent; |
|
90 } |
|
91 |
|
92 // Reset postIds, so a shortcode in a next post can be used |
|
93 self::$postIds = array(); |
|
94 |
|
95 return $content; |
|
96 } |
|
97 |
|
98 /** |
|
99 * Hooked on the admin's 'media_buttons' hook, outputs the shortcode inserter media button |
|
100 * |
|
101 * @since 2.1.16 |
|
102 */ |
|
103 static function shortcodeInserter(){ |
|
104 // Get slideshows |
|
105 $slideshows = new WP_Query(array( |
|
106 'post_type' => SlideshowPluginPostType::$postType, |
|
107 'orderby' => 'post_date', |
|
108 'order' => 'DESC' |
|
109 )); |
|
110 |
|
111 include(SlideshowPluginMain::getPluginPath() . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . __CLASS__ . DIRECTORY_SEPARATOR . 'shortcode-inserter.php'); |
|
112 } |
|
113 |
|
114 /** |
|
115 * Enqueues the shortcode inserter script |
|
116 * |
|
117 * @since 2.1.16 |
|
118 */ |
|
119 static function shortcodeInserterScript(){ |
|
120 wp_enqueue_script( |
|
121 'slideshow-shortcode-inserter', |
|
122 SlideshowPluginMain::getPluginUrl() . '/js/' . __CLASS__ . '/shortcode-inserter.js', |
|
123 array('jquery') |
|
124 ); |
|
125 |
|
126 wp_localize_script( |
|
127 'slideshow-shortcode-inserter', |
|
128 'SlideshowShortcodeInserter', |
|
129 array( |
|
130 'undefinedSlideshowMessage' => __('No slideshow selected.', 'slideshow-plugin'), |
|
131 'shortcode' => SlideshowPluginShortcode::$shortCode |
|
132 ) |
|
133 ); |
|
134 } |
|
135 } |