gde_write_profile
Create/update profile.
Description
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
if ( !function_exists( 'gde_write_profile' ) ) { require_once '/functions-admin.php'; } // The data. $data = null; // The id. $id = null; // The overwrite. $overwrite = false; // NOTICE! Understand what this does before running. $result = gde_write_profile($data, $id, $overwrite);
Defined (1)
The function is defined in the following location(s).
- /functions-admin.php
- function gde_write_profile( $data, $id = null, $overwrite = false ) {
- global $wpdb;
- $table = $wpdb->prefix . 'gde_profiles';
- if ( empty( $id ) ) {
- // get profile name
- $pname = strtolower( $data[0] );
- // new (non-default) profile
- if ( ! $wpdb->insert(
- $table,
- array(
- 'profile_name' => $pname,
- 'profile_desc' => $data[1],
- 'profile_data' => $data[2]
- )
- ) ) {
- gde_dx_log("Failed to create profile '$pname'");
- return 0;
- } else {
- gde_dx_log("New profile '$pname' created");
- return 1;
- }
- } else {
- // new (default) or updated profile
- if ( is_null( $wpdb->get_row( "SELECT * FROM $table WHERE profile_id = $id" ) ) ) {
- // new default profile
- //gde_dx_log("Profile ID $id doesn't exist - creating");
- if ( ! $wpdb->insert(
- $table,
- array(
- 'profile_id' => $id,
- 'profile_name' => strtolower( $data[0] ),
- 'profile_desc' => $data[1],
- 'profile_data' => $data[2]
- ),
- array(
- '%d', '%s', '%s', '%s'
- )
- ) ) {
- gde_dx_log("Profile $id creation failed");
- return 0;
- } else {
- gde_dx_log("Profile $id created");
- return 1;
- }
- } elseif ( $overwrite ) {
- // get old data
- $olddata = gde_get_profiles( $id, false, true );
- $olddesc = $olddata['profile_desc'];
- unset( $olddata['profile_desc'] );
- // update profile
- gde_dx_log("Profile ID $id exists - updating");
- if ( ! empty( $data[0] ) ) {
- // overwrite name
- $newdata['profile_name'] = strtolower( $data[0] );
- }
- if ( ! empty( $data[1] ) && ( $data[1] !== $olddesc ) ) {
- // overwrite description
- $newdata['profile_desc'] = $data[1];
- }
- if ( ! empty( $data[2] ) && ( $data[2] !== serialize( $olddata ) ) ) {
- // overwrite data
- $newdata['profile_data'] = $data[2];
- }
- if ( isset( $newdata ) ) {
- if ( ! $wpdb->update(
- $table,
- $newdata,
- array( 'profile_id' => $id ),
- array(
- '%s', '%s', '%s'
- )
- ) ) {
- $info = print_r($newdata, true);
- gde_dx_log("Profile $id update failed writing: \n\n $info");
- return 0;
- } else {
- gde_dx_log("Profile $id updated");
- return 2;
- }
- } else {
- gde_dx_log("Overwrite requested but no changes found");
- return 3;
- }
- } else {
- gde_dx_log("Profile $id exists, overwrite not specified - nothing changed");
- return 3;
- }
- }
- }