gde_write_profile

Create/update profile.

Description

(int) gde_write_profile( $data, (constant) $id = null, (bool) $overwrite = false ); 

Returns (int)

0 = fail, 1 = created, 2 = updated, 3 = nothing to do

Parameters (3)

0. $data
The data.
1. $id — Optional. (constant) => null
The id.
2. $overwrite — Optional. (bool) => false
The overwrite.

Usage

  1. if ( !function_exists( 'gde_write_profile' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-doc-embedder/functions-admin.php'; 
  3.  
  4. // The data. 
  5. $data = null; 
  6.  
  7. // The id. 
  8. $id = null; 
  9.  
  10. // The overwrite. 
  11. $overwrite = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = gde_write_profile($data, $id, $overwrite); 
  15.  

Defined (1)

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

/functions-admin.php  
  1. function gde_write_profile( $data, $id = null, $overwrite = false ) { 
  2. global $wpdb; 
  3. $table = $wpdb->prefix . 'gde_profiles'; 
  4.  
  5. if ( empty( $id ) ) { 
  6. // get profile name 
  7. $pname = strtolower( $data[0] ); 
  8.  
  9. // new (non-default) profile 
  10. if ( ! $wpdb->insert( 
  11. $table,  
  12. array( 
  13. 'profile_name' => $pname,  
  14. 'profile_desc' => $data[1],  
  15. 'profile_data' => $data[2] 
  16. ) ) { 
  17. gde_dx_log("Failed to create profile '$pname'"); 
  18. return 0; 
  19. } else { 
  20. gde_dx_log("New profile '$pname' created"); 
  21. return 1; 
  22. } else { 
  23. // new (default) or updated profile 
  24. if ( is_null( $wpdb->get_row( "SELECT * FROM $table WHERE profile_id = $id" ) ) ) { 
  25. // new default profile 
  26. //gde_dx_log("Profile ID $id doesn't exist - creating"); 
  27.  
  28. if ( ! $wpdb->insert( 
  29. $table,  
  30. array( 
  31. 'profile_id' => $id,  
  32. 'profile_name' => strtolower( $data[0] ),  
  33. 'profile_desc' => $data[1],  
  34. 'profile_data' => $data[2] 
  35. ),  
  36. array( 
  37. '%d', '%s', '%s', '%s' 
  38. ) ) { 
  39. gde_dx_log("Profile $id creation failed"); 
  40. return 0; 
  41. } else { 
  42. gde_dx_log("Profile $id created"); 
  43. return 1; 
  44. } elseif ( $overwrite ) { 
  45. // get old data 
  46. $olddata = gde_get_profiles( $id, false, true ); 
  47. $olddesc = $olddata['profile_desc']; 
  48. unset( $olddata['profile_desc'] ); 
  49.  
  50. // update profile 
  51. gde_dx_log("Profile ID $id exists - updating"); 
  52.  
  53. if ( ! empty( $data[0] ) ) { 
  54. // overwrite name 
  55. $newdata['profile_name'] = strtolower( $data[0] ); 
  56. if ( ! empty( $data[1] ) && ( $data[1] !== $olddesc ) ) { 
  57. // overwrite description 
  58. $newdata['profile_desc'] = $data[1]; 
  59.  
  60. if ( ! empty( $data[2] ) && ( $data[2] !== serialize( $olddata ) ) ) { 
  61. // overwrite data 
  62. $newdata['profile_data'] = $data[2]; 
  63.  
  64. if ( isset( $newdata ) ) { 
  65. if ( ! $wpdb->update( 
  66. $table,  
  67. $newdata,  
  68. array( 'profile_id' => $id ),  
  69. array( 
  70. '%s', '%s', '%s' 
  71. ) ) { 
  72. $info = print_r($newdata, true); 
  73. gde_dx_log("Profile $id update failed writing: \n\n $info"); 
  74. return 0; 
  75. } else { 
  76. gde_dx_log("Profile $id updated"); 
  77. return 2; 
  78. } else { 
  79. gde_dx_log("Overwrite requested but no changes found"); 
  80. return 3; 
  81. } else { 
  82. gde_dx_log("Profile $id exists, overwrite not specified - nothing changed"); 
  83. return 3;