web/wp-content/plugins/slideshow-jquery-image-gallery/classes/SlideshowPluginSlideInserter.php
changeset 194 32102edaa81b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php
       
     2 /**
       
     3  * Class SlideshowPluginSlideInserter
       
     4  *
       
     5  * TODO This class will probably need to be renamed to SlideshowPluginSlideHandler to explain more functionality
       
     6  * TODO than just inserting slides. (Show and delete functionality should be applied here as well)
       
     7  * @author Stefan Boonstra
       
     8  * @version 03-10-2012
       
     9  */
       
    10 class SlideshowPluginSlideInserter {
       
    11 
       
    12 	/** Flag to see if enqueue function has been called */
       
    13 	private static $enqueuedFiles;
       
    14 
       
    15 	/**
       
    16 	 * Returns the html for showing the image insert button.
       
    17 	 * Enqueues scripts unless $enqueueFiles is set to false.
       
    18 	 *
       
    19 	 * @param boolean $enqueueFiles
       
    20 	 * @return String $button
       
    21 	 */
       
    22 	static function getImageSlideInsertButton($enqueueFiles = true){
       
    23 		if($enqueueFiles)
       
    24 			self::enqueueFiles();
       
    25 
       
    26 		// Put popup html in footer
       
    27 		add_action('admin_footer', array(__CLASS__, 'includePopup'));
       
    28 
       
    29 		// Return button html
       
    30 		ob_start();
       
    31 		include(SlideshowPluginMain::getPluginPath() . '/views/' . __CLASS__ . '/insert-image-button.php');
       
    32 		return ob_get_clean();
       
    33 	}
       
    34 
       
    35 	/**
       
    36 	 * Returns the html for showing the text insert button.
       
    37 	 * Enqueues scripts unless $enqueueFiles is set to false.
       
    38 	 *
       
    39 	 * @param boolean $enqueueFiles
       
    40 	 * @return String $button
       
    41 	 */
       
    42 	static function getTextSlideInsertButton($enqueueFiles = true){
       
    43 		if($enqueueFiles)
       
    44 			self::enqueueFiles();
       
    45 
       
    46 		// Return button html
       
    47 		ob_start();
       
    48 		include(SlideshowPluginMain::getPluginPath() . '/views/' . __CLASS__ . '/insert-text-button.php');
       
    49 		return ob_get_clean();
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * Returns the html for showing the video insert button.
       
    54 	 * Enqueues scripts unless $enqueueFiles is set to false.
       
    55 	 *
       
    56 	 * @param boolean $enqueueFiles
       
    57 	 * @return String $button
       
    58 	 */
       
    59 	static function getVideoSlideInsertButton($enqueueFiles = true){
       
    60 		if($enqueueFiles)
       
    61 			self::enqueueFiles();
       
    62 
       
    63 		// Return button html
       
    64 		ob_start();
       
    65 		include(SlideshowPluginMain::getPluginPath() . '/views/' . __CLASS__ . '/insert-video-button.php');
       
    66 		return ob_get_clean();
       
    67 	}
       
    68 
       
    69 	/**
       
    70 	 * This function is registered in the SlideshowPluginAjax class
       
    71 	 * and deletes slides with a particular $_POST['slideId']
       
    72 	 */
       
    73 	static function deleteSlide(){
       
    74 		if((!isset($_POST['slideId']) || !is_numeric($_POST['slideId'])) ||
       
    75 			(!isset($_POST['postId']) || !is_numeric($_POST['postId'])))
       
    76 			die;
       
    77 
       
    78 		$search = 'slide_' . $_POST['slideId'] . '_';
       
    79 		$settings = get_post_meta($_POST['postId'], 'settings', true);
       
    80 		if(is_array($settings) && count($settings) > 0){
       
    81 			foreach($settings as $key => $setting)
       
    82 				if(strtolower(substr($key, 0, strlen($search))) == strtolower($search))
       
    83 					unset($settings[$key]);
       
    84 		}
       
    85 		update_post_meta($_POST['postId'], 'settings', $settings);
       
    86 
       
    87 		die;
       
    88 	}
       
    89 
       
    90 	/**
       
    91 	 * This function is registered in the SlideshowPluginAjax class
       
    92 	 * and prints the results from the search query
       
    93 	 */
       
    94 	static function printSearchResults(){
       
    95 		// Numberposts and offset
       
    96 		$numberPosts = 10;
       
    97 		$offset = 0;
       
    98 		if(isset($_POST['offset']))
       
    99 			$offset = $_POST['offset'];
       
   100 
       
   101 		// Get attachments with a title alike the search string, needs to be filtered
       
   102 		add_filter('posts_where', array(__CLASS__, 'printSearchResultsWhereFilter'));
       
   103 		$attachments = get_posts(array(
       
   104 			'numberposts' => $numberPosts + 1,
       
   105 			'offset' => $offset,
       
   106 			'orderby' => 'post_date',
       
   107 			'order' => 'DESC',
       
   108 			'post_type' => 'attachment',
       
   109 			'suppress_filters' => false
       
   110 		));
       
   111 		remove_filter('posts_where', array(__CLASS__, 'printSearchResultsWhereFilter'));
       
   112 
       
   113 		// Check if there are enough attachments to print a 'Load more images' button
       
   114 		$loadMoreResults = false;
       
   115 		if(count($attachments) > $numberPosts){
       
   116 			array_pop($attachments);
       
   117 			$loadMoreResults = true;
       
   118 		}
       
   119 
       
   120 		// Print results to the screen
       
   121 		if(count($attachments) > 0){
       
   122 			foreach($attachments as $attachment){
       
   123 				$image = wp_get_attachment_image_src($attachment->ID);
       
   124 				if(!$image[3]) $image[0] = SlideshowPluginMain::getPluginUrl() . '/images/SlideshowPluginPostType/no-img.png';
       
   125 				echo '<tr valign="top">
       
   126 					<td class="image">
       
   127 						<img width="60" height="60" src="' . $image[0] . '" class="attachment" alt="' . $attachment->post_title . '" title="' . $attachment->post_title . '">
       
   128 					</td>
       
   129 					<td class="column-title">
       
   130 						<strong class="title">
       
   131 							' . $attachment->post_title . '
       
   132 						</strong>
       
   133 						<p class="description">' . $attachment->post_content . '</p>
       
   134 					</td>
       
   135 					<td class="insert-button">
       
   136 						<input
       
   137 							type="button"
       
   138 							id="' . $attachment->ID . '"
       
   139 							class="insert-attachment button-secondary"
       
   140 							value="' . __('Insert', 'slideshow-plugin') . '"
       
   141 						/>
       
   142 					</td>
       
   143 				</tr>';
       
   144 			}
       
   145 			if($loadMoreResults){
       
   146 				echo '<tr>
       
   147 					<td colspan="3" style="text-align: center;">
       
   148 						<button class="button-secondary load-more-results ' . ($offset + $numberPosts) . '" >
       
   149 							' . __('Load more results', 'slideshow-plugin') . '
       
   150 						</button>
       
   151 					</td>
       
   152 				</tr>';
       
   153 			}
       
   154 		} else {
       
   155 			echo '<tr>
       
   156 				<td colspan="3" style="text-align: center;">
       
   157 					<a href="' . admin_url() . 'media-new.php" target="_blank">
       
   158 						' . __('No images were found, click here to upload some.', 'slideshow-plugin') . '
       
   159 					</a>
       
   160 				</td>
       
   161 			</tr>';
       
   162 		}
       
   163 
       
   164 		die;
       
   165 	}
       
   166 
       
   167 	/**
       
   168 	 * Applies a where clause on the get_posts call from self::printSearchResults()
       
   169 	 *
       
   170 	 * @param string $where
       
   171 	 * @return string $where
       
   172 	 */
       
   173 	static function printSearchResultsWhereFilter($where){
       
   174 		global $wpdb;
       
   175 
       
   176 		if(isset($_POST['search']))
       
   177 			$where .= $wpdb->prepare(
       
   178 				" AND (post_title LIKE '%%%s%%' OR ID LIKE '%%%s%%') ",
       
   179 				$_POST['search'],
       
   180 				$_POST['search']
       
   181 			);
       
   182 
       
   183 		return $where;
       
   184 	}
       
   185 
       
   186 	/**
       
   187 	 * Include popup, needs to be called in the footer
       
   188 	 */
       
   189 	static function includePopup(){
       
   190 		include(SlideshowPluginMain::getPluginPath() . '/views/' . __CLASS__ . '/search-popup.php');
       
   191 	}
       
   192 
       
   193 	/**
       
   194 	 * Enqueues styles and scripts necessary for the media upload button.
       
   195 	 */
       
   196 	static function enqueueFiles(){
       
   197 		// Check if already enqueued
       
   198 		if(self::$enqueuedFiles)
       
   199 			return;
       
   200 
       
   201 		// Enqueue style
       
   202 		wp_enqueue_style(
       
   203 			'slideshow-slide-inserter',
       
   204 			SlideshowPluginMain::getPluginUrl() . '/style/' . __CLASS__ . '/slide-inserter.css'
       
   205 		);
       
   206 
       
   207 		// Enqueue insert button script
       
   208 		wp_enqueue_script(
       
   209 			'slideshow-slide-inserter',
       
   210 			SlideshowPluginMain::getPluginUrl() . '/js/' . __CLASS__ . '/slide-inserter.js',
       
   211 			array('jquery')
       
   212 		);
       
   213 
       
   214 		wp_localize_script(
       
   215 			'slideshow-slide-inserter',
       
   216 			'SlideInserterTranslations',
       
   217 			array(
       
   218 				'confirmMessage' => __('Are you sure you want to delete this slide?', 'slideshow-plugin')
       
   219 			)
       
   220 		);
       
   221 
       
   222 		// Set enqueued to true
       
   223 		self::$enqueuedFiles = true;
       
   224 	}
       
   225 }