WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-functions.php
Go to the documentation of this file.
1 <?php
17 // -----------------------------------------------------------------------------------------------------------------
18 // has/is zone
19 // -----------------------------------------------------------------------------------------------------------------
20 
33 function wpdk_is_bool( $str )
34 {
35  return !in_array( strtolower( $str ), array( '', 'false', '0', 'no', 'n', 'off', null ) );
36 }
37 
51 function wpdk_is_url( $url )
52 {
53  if ( !empty( $url ) && is_string( $url ) ) {
54  return ( '#' === substr( $url, 0, 1 ) || '/' === substr( $url, 0, 1 ) || 'http' === substr( $url, 0, 4 ) ||
55  false !== strpos( $url, '?' ) || false !== strpos( $url, '&' ) );
56  }
57  return false;
58 }
59 
73 function wpdk_is_infinity( $value )
74 {
75  _deprecated_function( __FUNCTION__, '1.2.0', 'WPDKMath::isInfinity()' );
76  return WPDKMath::isInfinity( $value );
77 }
78 
87 function wpdk_is_ajax()
88 {
89  if ( defined( 'DOING_AJAX' ) ) {
90  return true;
91  }
92  if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest'
93  ) {
94  return true;
95  }
96  else {
97  return false;
98  }
99 }
100 
101 
109 function wpdk_is_child( $parent = '' )
110 {
111  global $post;
112 
113  $parent_obj = get_post( $post->post_parent, ARRAY_A );
114  $parent = (string)$parent;
115  $parent_array = (array)$parent;
116 
117  if ( $parent_obj && isset( $parent_obj['ID'] ) ) {
118  if ( in_array( (string)$parent_obj['ID'], $parent_array ) ) {
119  return true;
120  }
121  elseif ( in_array( (string)$parent_obj['post_title'], $parent_array ) ) {
122  return true;
123  }
124  elseif ( in_array( (string)$parent_obj['post_name'], $parent_array ) ) {
125  return true;
126  }
127  else {
128  return false;
129  }
130  }
131  return false;
132 }
133 
134 // -----------------------------------------------------------------------------------------------------------------
135 // Sanitize
136 // -----------------------------------------------------------------------------------------------------------------
137 
148 function wpdk_sanitize_function( $key )
149 {
150  return str_replace( '-', '_', sanitize_key( $key ) );
151 }
152 
160 function wpdk_get_image_size( $name )
161 {
162  global $_wp_additional_image_sizes;
163  if ( isset( $_wp_additional_image_sizes[$name] ) ) {
164  return $_wp_additional_image_sizes[$name];
165  }
166  return false;
167 }
168 
178 function wpdk_checked( $haystack, $current, $echo = true )
179 {
180  if ( is_array( $haystack ) && in_array( $current, $haystack ) ) {
181  $current = $haystack = 1;
182  }
183  return checked( $haystack, $current, $echo );
184 }
185 
196 function wpdk_selected( $haystack, $current, $echo = true )
197 {
198  _deprecated_function( __FUNCTION__, '1.2.0', 'WPDKHTMLTagSelect::selected()' );
199 
200  if ( is_array( $haystack ) && in_array( $current, $haystack ) ) {
201  $current = $haystack = 1;
202  }
203  return selected( $haystack, $current, $echo );
204 }
205 
207 /*
208  * TODO Il recupero dell'id per la compatibilità WPML è del tutto simile a quello usato in wpdk_permalink_page_with_slug
209  * si potrebbe portare fuori visto che sarebbe anche il caso di creare una funzione generica al riguardo, tipo una:
210  * wpdk_page_with_slug() che restituisca appunto l'oggetto da cui recuperare tutto quello che serve.
211  */
213 
226 function wpdk_content_page_with_slug( $slug, $post_type, $alternative_slug = '' )
227 {
228  global $wpdb;
229 
230  $page = get_page_by_path( $slug, OBJECT, $post_type );
231 
232  if ( is_null( $page ) ) {
233  $page = get_page_by_path( $alternative_slug, OBJECT, $post_type );
234 
235  if ( is_null( $page ) ) {
236  /* WPML? */
237  if ( function_exists( 'icl_object_id' ) ) {
238  $sql = <<< SQL
239 SELECT ID FROM {$wpdb->posts}
240 WHERE post_name = '{$slug}'
241 AND post_type = '{$post_type}'
242 AND post_status = 'publish'
243 SQL;
244  $id = $wpdb->get_var( $sql );
245  $id = icl_object_id( $id, $post_type, true );
246  }
247  else {
248  return false;
249  }
250  }
251  else {
252  $id = $page->ID;
253  }
254 
255  $page = get_post( $id );
256  }
257 
258  return apply_filters( "the_content", $page->post_content );
259 }
260 
272 function wpdk_permalink_page_with_slug( $slug, $post_type = 'page' )
273 {
274  global $wpdb;
275 
276  /* Cerco la pagina. */
277  $page = get_page_by_path( $slug, OBJECT, $post_type );
278 
279  /* Se non la trovo, prima di restituire null eseguo un controllo per WPML. */
280  if ( is_null( $page ) ) {
281 
282  /* WPML? */
283  if ( function_exists( 'icl_object_id' ) ) {
284  $sql = <<< SQL
285 SELECT ID FROM {$wpdb->posts}
286 WHERE post_name = '{$slug}'
287 AND post_type = '{$post_type}'
288 AND post_status = 'publish'
289 SQL;
290  $id = $wpdb->get_var( $sql );
291  $id = icl_object_id( $id, $post_type, true );
292  }
293  else {
294  return false;
295  }
296  }
297  else {
298  $id = $page->ID;
299  }
300 
301  $permalink = get_permalink( $id );
302 
303  return trailingslashit( $permalink );
304 }
305 
320 function wpdk_delta_object( $last_version, $old_version )
321 {
322  _deprecated_function( __FUNCTION__, '1.2.0', 'WPDKObject::__delta()' );
323  return WPDKObject::__delta( $last_version, $old_version );
324 }
325 
335 function wpdk_get_image_in_post_content( $id_post )
336 {
337  _deprecated_function( __FUNCTION__, '1.3.1', '_WPDKPost::imageContent() or _WPDKPost::imageContentWithID()' );
338 
339  ob_start();
340  ob_end_clean();
341  $output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_post_field( 'post_content', $id_post ), $matches );
342  if ( !empty( $matches ) && is_array( $matches ) && isset( $matches[1][0] ) ) {
343  $display_img = $matches[1][0];
344  return $display_img;
345  }
346  return null;
347 }
348 
360 {
361  _deprecated_function( __FUNCTION__, '1.3.1', '_WPDKPost::thumbnail() or _WPDKPost::thumbnailWithID()' );
362  if ( function_exists( 'has_post_thumbnail' ) ) {
363  if ( has_post_thumbnail( $id_post ) ) {
364  $image = wp_get_attachment_image_src( get_post_thumbnail_id( $id_post ), 'full' );
365  return $image[0];
366  }
367  }
368  return null;
369 }
370 
381 {
382  _deprecated_function( __FUNCTION__, '1.3.1', '_WPDKPost::imageAttachmentsWithID() or _WPDKPost::imageAttachments()' );
383  if ( function_exists( 'wp_get_attachment_image' ) ) {
384  $children = get_children( array(
385  'post_parent' => $id_post,
386  'post_type' => 'attachment',
387  'numberposts' => 1,
388  'post_status' => 'inherit',
389  'post_mime_type' => 'image',
390  'order' => 'ASC',
391  'orderby' => 'menu_order ASC'
392  ) );
393 
394  if ( empty( $children ) || !is_array( $children ) ) {
395  return false;
396  }
397 
398  $item = current( $children );
399 
400  if ( is_object( $item ) && isset( $item->ID ) ) {
401  $image = wp_get_attachment_image_src( $item->ID, 'full' );
402  return $image[0];
403  }
404  }
405  return false;
406 }
407 
408 // -----------------------------------------------------------------------------------------------------------------
409 // WPDKResult check
410 // -----------------------------------------------------------------------------------------------------------------
411 
425 function is_wpdk_error( $thing )
426 {
427  _deprecated_function( __FUNCTION__, '1.2.0', 'WPDKResult::isError()' );
428  return WPDKResult::isError( $thing );
429 }
430 
443 function is_wpdk_warning( $thing )
444 {
445  _deprecated_function( __FUNCTION__, '1.2.0', 'WPDKResult::isWarning()' );
446  return WPDKResult::isWarning( $thing );
447 }
448 
461 function is_wpdk_status( $thing )
462 {
463  _deprecated_function( __FUNCTION__, '1.2.0', 'WPDKResult::isStatus()' );
464  return WPDKResult::isWarning( $thing );
465 }
466 
467 // -----------------------------------------------------------------------------------------------------------------
468 // WPDKResult check
469 // -----------------------------------------------------------------------------------------------------------------
470 
485 function wpdk_add_page( $page_slug, $page_title, $capability, $function = '', $hook_head = '', $hook_load = '' )
486 {
487  global $admin_page_hooks, $_registered_pages, $_parent_pages;
488 
489  $hookname = '';
490 
491  if ( !empty( $function ) && current_user_can( $capability ) ) {
492  $page_slug = plugin_basename( $page_slug );
493  $admin_page_hooks[$page_slug] = $page_title;
494  $hookname = get_plugin_page_hookname( $page_slug, '' );
495  if ( !empty( $hookname ) ) {
496  add_action( $hookname, $function );
497  $_registered_pages[$hookname] = true;
498  $_parent_pages[$page_slug] = false;
499 
500  if ( !empty( $hook_head ) ) {
501  add_action( 'admin_head-' . $hookname, $hook_head );
502  }
503 
504  if ( !empty( $hook_load ) ) {
505  add_action( 'load-' . $hookname, $hook_load );
506  }
507  }
508  }
509  return $hookname;
510 }
511 
525 function wpdk_enqueue_script_page( $pages, $handle, $src = false, $deps = array(), $ver = false, $in_footer = false )
526 {
527  foreach ( $pages as $slug ) {
528  if ( is_page_template( $slug ) ) {
529  wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
530  break;
531  }
532  }
533 }
534 
550 function wpdk_enqueue_script_page_template( $page_templates, $handle, $src = false, $deps = array(), $ver = false, $in_footer = false )
551 {
552  if ( empty( $page_templates ) ) {
553  return false;
554  }
555  if ( is_string( $page_templates ) ) {
556  $page_templates = array( $page_templates );
557  }
558  foreach ( $page_templates as $slug ) {
559  if ( is_page_template( $slug ) ) {
560  wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
561  break;
562  }
563  }
564  return true;
565 }
566 
587 function wpdk_set_user_transient( $transient, $value, $expiration = 0, $user_id = null )
588 {
589  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.3.0', 'WPDKUser::setTransientWithUser()' );
590  return WPDKUser::setTransientWithUser( $transient, $value, $expiration, $user_id );
591 }
592 
610 function wpdk_get_user_transient( $transient, $user_id = null )
611 {
612  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.4.8', 'WPDKUser::getTransientWithUser()' );
613  return WPDKUser::getTransientWithUser( $transient, $user_id );
614 }
615 
630 function wpdk_delete_user_transient( $transient, $user_id = null )
631 {
632 
633  $user_id = is_null( $user_id ) ? get_current_user_id() : $user_id;
634 
635  do_action( 'delete_user_transient_' . $transient, $transient, $user_id );
636 
637  $transient_timeout = '_transient_timeout_' . $transient;
638  $transient = '_transient_' . $transient;
639  $result = delete_user_meta( $user_id, $transient );
640  if ( $result ) {
641  delete_user_meta( $user_id, $transient_timeout );
642  do_action( 'deleted_user_transient', $transient, $user_id );
643  }
644 
645  return $result;
646 }
647 
658 function wpdk_is_request( $verb )
659 {
660  $verb = strtolower( $verb );
661  return ( $verb == strtolower( $_SERVER['REQUEST_METHOD'] ) );
662 }
663 
672 {
673  return wpdk_is_request( 'get' );
674 }
675 
684 {
685  return wpdk_is_request( 'post' );
686 }