PluginProbe
oik / 1.7
oik v1.7
1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.2 1.3 1.3.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.0.1 2.1 2.2 2.3 2.4 2.5 2.5.1 All 71 releases
oik / oik-pages.php

oik-pages.php in oik 1.7, at oik-pages.php

460 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Plugin Name: oik pages
5 Plugin URI: http://www.oik-plugins.com/oik
6 Description: [bw_pages], [bw_list] and [bw_bookmarks] shortcodes to summarize child pages or custom post types
7 Version: 1.7
8 Author: bobbingwide
9 Author URI: http://www.bobbingwide.com
10 License: GPL2
11
12 Copyright 2011 Bobbing Wide (email : herb@bobbingwide.com )
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License version 2,
16 as published by the Free Software Foundation.
17
18 You may NOT assume that you can use any other version of the GPL.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 The license for this software can likely be found here:
26 http://www.gnu.org/licenses/gpl-2.0.html
27
28 */
29
30 require_once( 'bobbfunc.inc' );
31 require_once( 'bobbingwide.inc' );
32 /* This include will enable oik shortcodes even if the oik base is not enabled. Is this is good idea? */
33 require_once( 'oik-add-shortcodes.php' );
34 /* Include functions to determine level of Artisteer theme whilst Artisteer doesn't provide it */
35 require_once( 'oik-artisteer.php' );
36
37
38 bw_add_shortcode( 'bw_pages', 'bw_pages' );
39 bw_add_shortcode( 'bw_list', 'bw_list' );
40 bw_add_shortcode( 'bw_bookmarks', 'bw_bookmarks' );
41
42
43 /**
44 * Return the excerpt from the $post
45 * Note: The function _theme_get_excerpt is not expected to exist.
46 * Artisteer doesn't "properly" extract the excerpt, so the '_' prefix disables the call to the Artisteer function.
47 *
48 * @param post $post_id post from which to extract the excerpt
49 * @return string $excerpt the excerpt from the post
50 *
51 */
52 function bw_excerpt( $post_id ) {
53 if ( function_exists( '_theme_get_excerpt' ) ) {
54
55 global $post;
56 $save_post = $post;
57 $post = $post_id;
58 $excerpt = theme_get_excerpt();
59 $post = $save_post;
60
61 } elseif ( empty( $post_id->excerpt ) ) {
62 $content = explode( '<!--more-->', $post_id->post_content);
63 $excerpt = $content[0];
64 } else {
65 $excerpt = $post_id->excerpt;
66 }
67 return( $excerpt );
68 }
69
70 /**
71 * Return an array suitable for passing to image functions to determine the size
72 *
73 * @param mixed $size string representing the size.
74 * if a single integer then make the array square
75 * otherwise it's widthxheight or width,height or some other way of specifying width and height
76 * so we split at the non numeric value(s) and take the first two integer bits
77 * @return array containing width and height
78 */
79 function bw_get_image_size( $size=100 ) {
80 $pattern = "/([\d]+)/";
81 preg_match_all( $pattern, $size, $thumbnail );
82 // bw_trace2( $thumbnail );
83 if ( count( $thumbnail[0] ) < 2 )
84 $thumbnail[0][1] = $thumbnail[0][0];
85
86 $size = array( $thumbnail[0][0], $thumbnail[0][1] );
87
88 // bw_trace( $size, __FUNCTION__, __LINE__, __FILE__, "size" );
89 return( $size );
90 }
91
92
93 /**
94 * get the thumbnail of the specified size
95 *
96 */
97 function bw_thumbnail( $post_id, $atts=NULL ) {
98 bw_trace( $post_id, __FUNCTION__, __LINE__, __FILE__, "post_id" );
99
100 $thumbnail = bw_array_get( $atts, 'thumbnail', 'thumbnail' );
101 switch ( $thumbnail ) {
102 case 'thumbnail':
103 case 'medium':
104 case 'large':
105 $torf = TRUE;
106 break;
107
108 default:
109 $torf = bw_validate_torf( $thumbnail );
110 if ( $torf ) {
111 $thumbnail = 'thumbnail';
112 } else {
113 $thumbnail = bw_get_image_size( $thumbnail );
114 }
115 break;
116 }
117
118 /* Cater for Artisteer themes that already return thumbnails */
119
120 if ( function_exists( '_theme_get_post_thumbnail') ) {
121 global $post;
122 $save_post = $post;
123 $post = $post_id;
124
125 $thumbnail = theme_get_post_thumbnail();
126 $post = $save_post;
127
128 } else {
129 $thumbnail = bw_get_thumbnail( $post_id, $thumbnail, $atts );
130 }
131 return( $thumbnail );
132 }
133
134 /**
135 * Create a thumbnail link
136 *
137 * Create a thumbnail with a link to the post_id specified, either via $post_id or the $atts['link']
138 * otherwise just create the image
139 *
140 * @param string $thumbnail full HTML for the thumbnail image
141 * @param id $post_id default post id if not specified in $atts
142 * @param array $atts shortcode attributes array
143 */
144 function bw_link_thumbnail( $thumbnail, $post_id=NULL, $atts=NULL ) {
145 $link_id = bw_array_get( $atts, "link", $post_id );
146 if ( $link_id ) {
147
148 $text = bw_array_get( $atts, "title", NULL );
149 alink( NULL, get_permalink( $link_id ), $thumbnail, $text, "link-".$link_id );
150 } else {
151 e( $thumbnail );
152 }
153 }
154
155 /**
156 * Format the "post" - basic first version
157 *
158 * Format the 'post' in a block or div with title, image with link, excerpt and read more button
159 *
160 * @param object $post - A post object
161 * @param array $atts - Attributes array - passed from the shortcode
162 *
163 *
164 */
165 function bw_format_post( $post, $atts ) {
166 setup_postdata( $post );
167
168 bw_trace( $post, __FUNCTION__, __LINE__, __FILE__, "post" );
169
170 $atts['title'] = get_the_title( $post->ID );
171 $read_more = bw_array_get( $atts, "read_more", "read more" );
172 $thumbnail = bw_thumbnail( $post->ID, $atts );
173
174 $in_block = bw_validate_torf( bw_array_get( $atts, "block", TRUE ));
175 if ( $in_block ) {
176 e( bw_block( $atts ));
177 sdiv( "avatar alignleft" );
178 bw_link_thumbnail( $thumbnail, $post->ID, $atts );
179 ediv();
180 } else {
181 $class = bw_array_get( $atts, "class", "" );
182 sdiv( $class );
183 sdiv( "avatar alignleft" );
184 bw_link_thumbnail( $thumbnail, $post->ID, $atts );
185 ediv();
186 span( "title" );
187 strong( $atts['title'] );
188 epan();
189 br();
190 }
191 e( bw_excerpt( $post ) );
192 sp();
193 art_button( get_permalink( $post->ID ), $read_more, $read_more );
194 ep();
195 if ( $in_block )
196 e( bw_eblock() );
197 else {
198 sediv( "cleared" );
199 ediv();
200 }
201 }
202
203
204 /**
205 * Format the "post" - in a simple list item list
206 */
207 function bw_format_list( $post, $atts ) {
208 setup_postdata( $post );
209
210 // bw_trace( $post, __FUNCTION__, __LINE__, __FILE__, "post" );
211 $title = get_the_title( $post->ID );
212 stag( 'li' );
213 alink( NULL, get_permalink( $post->ID ), $title );
214 etag( 'li' );
215
216 }
217
218
219 /**
220 * List sub-pages of the current or selected page
221 *
222 * This function is designed to replace the functionality of the [bw_plug name='extended-page-lists'] plugin and other plugins that list pages.
223 * It works in conjunction with Artisteer blocks - to enable the page list to be styled as a series of blocks
224 * Well, that's the plan
225 *
226 * [bw_pages class="classes for bw_block"
227 * post_type='page'
228 * post_parent
229 * orderby='title'
230 * order='ASC'
231 * posts_per_page=-1
232 * block=true or false
233 * thumbnail=specification - see bw_thumbnail
234 * customcategoryname=custom category value
235 */
236 function bw_pages( $atts = NULL ) {
237 $posts = bw_get_posts( $atts );
238 bw_trace( $posts, __FUNCTION__, __LINE__, __FILE__, "posts" );
239
240 foreach ( $posts as $post ) {
241 bw_format_post( $post, $atts );
242 }
243
244 return( bw_ret() );
245 }
246
247 /**
248 * Force the thumbnail size to be what we asked for
249 *
250 * wp_attachment_get_image_src() can sometimes attempt to return a $thumbnail where the
251 * width and height don't match what we asked for.
252 *
253 * The values of $thumbnail[1] and $thumbnail[2] are used to set the width and height values on the <img> tag
254 * We choose to reset these to the values first thought of.
255 *
256 * Note: The side effect of this is that the images get reshaped rather than being cropped.
257 * It's up to the web designer to set the sizes for thumbnail, small, medium and large
258 * and/or upload images that will scale nicely for the chosen figures.
259 */
260 function bw_force_size( $thumbnail, $size ) {
261 if ( is_array( $size ) ) {
262
263 $thumbnail[1] = $size[0];
264 $thumbnail[2] = $size[1];
265 }
266 return( $thumbnail );
267 }
268
269
270 /**
271 * get the post thumbnail
272 * Returns the HTML for the thumbnail image which can then be wrapped in a link if required
273 * @param integer $post_id - the id of the content for which the thumbnail is required
274 * defaults to the current post id
275 * @param mixed $size - the required image size: either a preset or specified in an array
276 * @atts array array of key value pairs that may be needed
277 *
278 * Return Value: An array containing:
279 * $image[0] => url
280 * $image[1] => width
281 * $image[2] => height
282 *
283 */
284 function bw_get_thumbnail( $post_id = null, $size = 'thumbnail', $atts=NULL ) {
285 $return_value = FALSE;
286 if ($post_id == null)
287 $post_id = get_the_id();
288
289 bw_trace( $post_id, __FUNCTION__, __LINE__, __FILE__, "post_id" );
290
291 if ( has_post_thumbnail( $post_id ) ) {
292 //bw_trace( $size, __FUNCTION__, __LINE__, __FILE__, "size" );
293 $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
294 }
295 elseif ( function_exists('get_post_thumbnail_id') && $thumb_id = get_post_thumbnail_id( $post_id ) ) {
296 // bw_trace( $thumb_id, __FUNCTION__, __LINE__, __FILE__, "thumb_id" );
297 $thumbnail = wp_get_attachment_image_src( $thumb_id, $size ) ;
298 }
299 elseif ( $arr_thumb = bw_get_attached_image( $post_id, 1, 'rand', $size )) {
300 //bw_trace( $arr_thumb, __FUNCTION__, __LINE__, __FILE__, "arr_thumb" );
301 $thumbnail = $arr_thumb[0];
302 }
303 // bw_trace( $thumbnail, __FUNCTION__, __LINE__, __FILE__, "thumbnail" );
304 if ( $thumbnail[0] ) {
305 $text = bw_array_get( $atts, "title", NULL );
306 $thumbnail = bw_force_size( $thumbnail, $size );
307
308 $return_value = retimage( "bw_thumbnail", $thumbnail[0], $text , $thumbnail[1], $thumbnail[2] );
309 }
310 bw_trace( $return_value, __FUNCTION__, __LINE__, __FILE__, "return_value" );
311 return( $return_value );
312 }
313
314 /**
315 * get the attached image
316 *
317 * Return an array of images attached to a specific post ID
318 *
319 *
320 * Return Value: An array containing:
321 * $image[0] => url
322 * $image[1] => width
323 * $image[2] => height
324 * $image[3] => attachment id
325 */
326 function bw_get_attached_image( $post_id = null, $number = 1, $orderby = 'rand', $image_size = 'thumbnail') {
327
328 if ($post_id == null)
329 $post_id = get_the_id();
330 bw_trace( $post_id, __FUNCTION__, __LINE__, __FILE__, "post_id" );
331 $number = intval( $number );
332
333 $arr_attachment = get_posts (array( 'post_parent' => $post_id,
334 'post_type' => 'attachment',
335 'numberposts' => $number,
336 'post_mime_type' => 'image',
337 'orderby' => $orderby ));
338
339 foreach ( $arr_attachment as $index => $attachment ) {
340 $arr_attachment[$index] = array_merge ( (array) wp_get_attachment_image_src($attachment->ID, $image_size), array($attachment->ID) );
341 }
342
343 bw_trace( $arr_attachment, __FUNCTION__, __LINE__, __FILE__, "arr_attachment" );
344
345 return $arr_attachment;
346 }
347
348
349 /**
350 * List sub-pages of the current or selected page - in a simple list
351 *
352 * Same as bw_pages but producing a simple list of links to the content type
353 *
354 *
355 * [bw_list class="classes for the list"
356 * post_type='page'
357 * post_parent
358 * orderby='title'
359 * order='ASC'
360 * posts_per_page=-1
361 * block=true or false
362 * thumbnail=specification - see bw_thumbnail
363 * customcategoryname=custom category value
364 */
365 function bw_list( $atts = NULL ) {
366
367 $posts = bw_get_posts( $atts );
368
369 sul( bw_array_get( $atts, 'class', 'bw_list' ));
370
371 foreach ( $posts as $post ) {
372 bw_format_list( $post, $atts );
373 }
374 eul();
375
376 return( bw_ret() );
377 }
378
379 /**
380 * Wrapper to get_posts()
381 *
382 * When no parameters are passed processing should depend upon the context
383 * e.g for a Page it should list the child pages
384 * for a post it should show related posts in the same category as the current post
385 * TO BE COMPLETED
386 */
387 function bw_get_posts( $atts = NULL ) {
388 // Copy the atts from the shortcode to create the array for the query
389 // removing the class and title parameter that gets passed to bw_block()
390
391 $attr = $atts;
392 bw_trace( $atts, __FUNCTION__, __LINE__, __FILE__, "atts" );
393 //bw_trace( $attr, __FUNCTION__, __LINE__, __FILE__, "attr" );
394 /* Set default values if not already set */
395
396 // Set the post_type attribute - initially default to NULL
397 $attr['post_type'] = bw_array_get( $attr, 'post_type', NULL );
398
399 // Only default post_parent for post_type of 'page'
400 // This allows [bw_pages] to be used without parameters on a page
401 // and to be used to list 'page's from other post types.
402 //
403 if ( $attr['post_type'] == 'page' ) {
404 $attr['post_parent'] = bw_array_get( $attr, "post_parent", $GLOBALS['post']->ID );
405 } else {
406 // Now let it default to page if still NULL
407 $attr['post_type'] = bw_array_get( $attr, 'post_type', 'page' );
408 }
409 $attr['numberposts'] = bw_array_get( $attr, "numberposts", -1 );
410 $attr['orderby'] = bw_array_get( $attr, "orderby", "title" );
411 $attr['order'] = bw_array_get( $attr, "order", "ASC" );
412 $attr['category_name'] = bw_array_get( $attr, "category_name", NULL );
413
414 // Regardless of the post type, exclude the current post,
415 // Note: This could also be improved **?**
416 $attr['exclude'] = bw_array_get( $attr, "exclude", $GLOBALS['post']->ID );
417
418 bw_trace( $attr, __FUNCTION__, __LINE__, __FILE__, "attr" );
419
420 //if ( $attr['post_type'] == 'post' ) {
421 $posts = get_posts( $attr );
422 //} else {
423 // $posts = get_pages( $attr );
424 // }
425 bw_trace( $posts, __FUNCTION__, __LINE__, __FILE__, "posts" );
426 return( $posts );
427
428 }
429
430
431 /**
432 * Wrapper to wp_list_bookmarks()
433 *
434 * which replaces get_links()
435 */
436 function bw_bookmarks( $atts = NULL ) {
437 // Copy the atts from the shortcode to create the array for the query
438 // removing the class and title parameter that gets passed to bw_block()
439
440 $attr = $atts;
441 bw_trace( $atts, __FUNCTION__, __LINE__, __FILE__, "atts" );
442 //bw_trace( $attr, __FUNCTION__, __LINE__, __FILE__, "attr" );
443 /* Set default values if not already set */
444
445 $attr['limit'] = bw_array_get( $attr, "numberposts", -1 );
446 $attr['orderby'] = bw_array_get( $attr, "orderby", "name" );
447 $attr['order'] = bw_array_get( $attr, "order", "ASC" );
448 $attr['category_name'] = bw_array_get( $attr, "category_name", NULL );
449 // $attr['exclude'] = bw_array_get( $attr, "exclude", $GLOBALS['post']->ID );
450 $attr['echo'] = 0;
451
452 bw_trace( $attr, __FUNCTION__, __LINE__, __FILE__, "attr" );
453 $posts = wp_list_bookmarks( $attr );
454 bw_trace( $posts, __FUNCTION__, __LINE__, __FILE__, "posts" );
455 return( $posts );
456
457 }
458
459
460