pods_unique_slug

Build a unique slug.

Description

(string) pods_unique_slug( (string) $slug, (string) $column_name, (string|array) $pod, (int) $pod_id = 0, (int) $id = 0, (constant) $obj = null, (bool) $strict = true ); 

Returns (string)

The unique slug name

Parameters (7)

0. $slug (string)
The slug value
1. $column_name (string)
The column name.
2. $pod (string|array)
The Pod name or array of Pod data
3. $pod_id — Optional. (int)
The pod id.
4. $id — Optional. (int)
The item ID
5. $obj — Optional. (constant) => null
6. $strict — Optional. (bool) => true
The strict.

Usage

  1. if ( !function_exists( 'pods_unique_slug' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'pods-custom-content-types-and-fields/includes/data.php'; 
  3.  
  4. // The slug value 
  5. $slug = ''; 
  6.  
  7. // The column name. 
  8. $column_name = ''; 
  9.  
  10. // The Pod name or array of Pod data 
  11. $pod = null; 
  12.  
  13. // The pod id. 
  14. $pod_id = -1; 
  15.  
  16. // The item ID 
  17. $id = -1; 
  18.  
  19. // (optional) 
  20. $obj = null; 
  21.  
  22. // The strict. 
  23. $strict = true; 
  24.  
  25. // NOTICE! Understand what this does before running. 
  26. $result = pods_unique_slug($slug, $column_name, $pod, $pod_id, $id, $obj, $strict); 
  27.  

Defined (1)

The function is defined in the following location(s).

/includes/data.php  
  1. function pods_unique_slug ( $slug, $column_name, $pod, $pod_id = 0, $id = 0, $obj = null, $strict = true ) { 
  2. $slug = pods_create_slug( $slug, $strict ); 
  3.  
  4. $pod_data = array(); 
  5.  
  6. if ( is_array( $pod ) ) { 
  7. $pod_data = $pod; 
  8. $pod_id = pods_v_sanitized( 'id', $pod_data, 0 ); 
  9. $pod = pods_v_sanitized( 'name', $pod_data ); 
  10.  
  11. $pod_id = absint( $pod_id ); 
  12. $id = absint( $id ); 
  13.  
  14. if ( empty( $pod_data ) ) 
  15. $pod_data = pods_api()->load_pod( array( 'id' => $pod_id, 'name' => $pod ), false ); 
  16.  
  17. if ( empty( $pod_data ) || empty( $pod_id ) || empty( $pod ) ) 
  18. return $slug; 
  19.  
  20. if ( 'table' != $pod_data[ 'storage' ] || !in_array( $pod_data[ 'type' ], array( pod, 'table' ) ) ) 
  21. return $slug; 
  22.  
  23. $check_sql = " 
  24. SELECT DISTINCT `t`.`{$column_name}` AS `slug` 
  25. FROM `@wp_pods_{$pod}` AS `t` 
  26. WHERE `t`.`{$column_name}` = %s AND `t`.`id` != %d 
  27. LIMIT 1 
  28. "; 
  29.  
  30. $slug_check = pods_query( array( $check_sql, $slug, $id ), $obj ); 
  31.  
  32. if ( !empty( $slug_check ) || apply_filters( 'pods_unique_slug_is_bad_flat_slug', false, $slug, $column_name, $pod, $pod_id, $id, $pod_data, $obj ) ) { 
  33. $suffix = 2; 
  34.  
  35. do { 
  36. $alt_slug = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-{$suffix}"; 
  37.  
  38. $slug_check = pods_query( array( $check_sql, $alt_slug, $id ), $obj ); 
  39.  
  40. $suffix++; 
  41. while ( !empty( $slug_check ) || apply_filters( 'pods_unique_slug_is_bad_flat_slug', false, $alt_slug, $column_name, $pod, $pod_id, $id, $pod_data, $obj ) ); 
  42.  
  43. $slug = $alt_slug; 
  44.  
  45. $slug = apply_filters( 'pods_unique_slug', $slug, $id, $column_name, $pod, $pod_id, $obj ); 
  46.  
  47. return $slug;