Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Please confirm you want to mark all topics read

Mark all topics read

sp_TopicIcon
PHP/Wordpress Help?
No permission to create posts sp_Feed
Avatar
Jarandhel Dreamsinger
Arlington, VA
Posts: 540

Friday, 25th June, 2010 - 10:13 am
1

Ok, this is driving me out of my gourd so I'm going to post about it here and maybe someone will read it who understands this stuff better than I do (I do not claim any actual ability to program in PHP):

I am running the plugin Project Manager for WordPress.  It's a very nice plugin that (among other things) allows for extending user profiles with extra data that can then be displayed in a searchable table.  It's what the Otherkin Directory section of the site is being built on.  And mostly, it's working great.

The problem comes in with the creation of new users.  It's supposed to create a new dataset automatically whenever a new user is created whose role allows them to use the profile hook.  The default role on this website for new users does allow them to use that hook.  And when I create a new account from the admin panel, it works fine.

However, when I create a new account as if I was a new user registering on the site?  No new dataset is created.

Sounds like a permission issue, right?  Well, here's where it gets weirder.

Right now, the plugin is hooking into the action user_register, which is called whenever a new user account is created (either by the admin or by a new user registering).  When that action occurs, the plugin runs a function to create a new dataset.

If I make a copy of that function call, and tell it to hook into the action profile_update, which is called whenever a user updates their profile, newly created users (including the ones for whom the user_register hook failed once already) have a dataset created for them if there is not one associated with their account already.  This hook works 100% of the time.

Same user, same permissions.  Two hooks that should both be called 100% of the time.  Only one is working reliably on the user side.  Thoughts?  I don't even know what could be causing this behavior at this point.  I also tried hooking it into wp_new_user_notification and get_user_meta, both of which seemed to work but only intermittently, if that provides any useful data.  Frankly the intermittent aspect just confuses me even more.

Btw, I have also tested this on a clean install of wordpress with no other plugins active and experienced the same issues.  So right away we should be able to rule out plugin conflicts or anything that would be particular to my setup.

AnOtherWiki: A free encyclopedia by, for, and about Otherkin.  Join us, and help us grow.

 

Avatar
Jarandhel Dreamsinger
Arlington, VA
Posts: 540

Friday, 25th June, 2010 - 2:20 pm
2

And the actual code:

from projectmanager.php, the actions which call the register user function (the first is the app's original one, the second is the one added by me which works 100% of the time but requires the user to update their profile):

add_action( 'user_register', array(&$projectmanager, 'registerUser') );
add_action( 'profile_update', array(&$projectmanager, 'registerUser') );

 

from core.php, the registerUser function:

/**
* registrer new user
*
* @param int $user_id
* @return void
*/
function registerUser( $user_id )
{
require_once( PROJECTMANAGER_PATH.'/admin/admin.php' );
$admin = new ProjectManagerAdminPanel();
$user = new WP_User($user_id);
if ( $user->has_cap('projectmanager_user') ) {
foreach ( $this->getProjects() AS $project ) {
if ( 1 == $project->profile_hook )
$admin->addDataset( $project->id, $user->first_name, array(), false, $user_id );
}
}
}

 

And from admin.php, the addDataset function called by registerUser:

/**
* add new dataset
*
* @param int $project_id
* @param string $name
* @param array $cat_ids
* @param array $dataset_meta
* @param false|int $user_id
* @return string
*/
function addDataset( $project_id, $name, $cat_ids, $dataset_meta = false, $user_id = false )
{
global $wpdb, $current_user, $projectmanager;
if ( $user_id && $this->datasetExists($project_id, $user_id) ) {
$this->setMessage( __( 'You cannot add two datasets with same User ID.', 'projectmanager' ), true );
return false;
}
$projectmanager->initialize($project_id);
$this->project_id = $project_id;
$project = $this->project = $projectmanager->getProject($project_id);
if ( !$user_id ) $user_id = $current_user->ID;
// Negative check on capability: user can't edit datasets
if ( !current_user_can('edit_datasets') && !current_user_can('projectmanager_user') && !current_user_can('import_datasets') ) {
$this->setMessage( __(“You don't have permission to perform this task”, 'projectmanager'), true );
return;
}
// user has only cap 'projectmanager_user' but not 'edit_other_datasets' and 'edit_datasets'
if ( current_user_can('projectmanager_user') && !current_user_can('edit_other_datasets') && !current_user_can('edit_datasets') && !current_user_can('import_datasets') ) {
// and dataset with this user ID already exists
if ( $this->datasetExists($project_id, $user_id) ) {
$this->setMessage( __(“You don't have permission to perform this task”, 'projectmanager'), true );
return;
}
}
$wpdb->query( $wpdb->prepare( “INSERT INTO {$wpdb->projectmanager_dataset} (name, cat_ids, project_id, user_id) VALUES ('%s', '%s', '%d', '%d')”, $name, maybe_serialize($cat_ids), $project_id, $user_id ) );
$dataset_id = $wpdb->insert_id;
 

if ( $dataset_meta ) {
foreach ( $dataset_meta AS $meta_id => $meta_value ) {
$formfield = parent::getFormFields($meta_id);

// Manage file upload
if ( 'file' == $formfield->type || 'image' == $formfield->type || 'video' == $formfield->type ) {
$file = array('name' => $_FILES['form_field']['name'][$meta_id], 'tmp_name' => $_FILES['form_field']['tmp_name'][$meta_id], 'size' => $_FILES['form_field']['size'][$meta_id], 'type' => $_FILES['form_field']['type'][$meta_id]);
if ( !empty($file['name']) )
$this->uploadFile($file);
$meta_value = basename($file['name']);
// Create Thumbails for Image
if ( 'image' == $formfield->type && !empty($meta_value) ) {
$new_file = parent::getFilePath().'/'.$meta_value;
$image = new ProjectManagerImage($new_file);
// Resize original file and create thumbnails
$dims = array( 'width' => $project->medium_size['width'], 'height' => $project->medium_size['height'] );
$image->createThumbnail( $dims, $new_file, $project->chmod );
$dims = array( 'width' => $project->thumb_size['width'], 'height' => $project->thumb_size['height'] );
$image->createThumbnail( $dims, parent::getFilePath().'/thumb.'.$meta_value, $project->chmod );

$dims = array( 'width' => 80, 'height' => 50 );
$image->createThumbnail( $dims, parent::getFilePath().'/tiny.'.$meta_value, $project->chmod );
}
} elseif ( 'numeric' == $formfield->type || 'currency' == $formfiel->type ) {
$meta_value += 0; // convert value to numeric type
}

if ( is_array($meta_value) ) {
// form field value is a date
if ( array_key_exists('day', $meta_value) && array_key_exists('month', $meta_value) && array_key_exists('year', $meta_value) ) {
$meta_value = sprintf(“%s-%s-%s”, $meta_value['year'], $meta_value['month'], $meta_value['day']);
} elseif ( array_key_exists('hour', $meta_value) && array_key_exists('minute', $meta_value) ) {
$meta_value = sprintf(“%s:%s”, $meta_value['hour'], $meta_value['minute']);
}
}
$wpdb->query( $wpdb->prepare( “INSERT INTO {$wpdb->projectmanager_datasetmeta} (form_id, dataset_id, value) VALUES ('%d', '%d', '%s')”, $meta_id, $dataset_id, maybe_serialize($meta_value) ) );
}

// Check for unsubmitted form data, e.g. checkbox list
if ($form_fields = parent::getFormFields()) {
foreach ( $form_fields AS $form_field ) {
if ( !array_key_exists($form_field->id, $dataset_meta) ) {
$wpdb->query( $wpdb->prepare( “INSERT INTO {$wpdb->projectmanager_datasetmeta} (form_id, dataset_id, value) VALUES ('%d', '%d', '')”, $dataset_id, $form_field->id ) );
}
}
}
} else {
// Populate empty meta value for new registered user
foreach ( $projectmanager->getFormFields() AS $formfield ) {
$wpdb->query( $wpdb->prepare( “INSERT INTO {$wpdb->projectmanager_datasetmeta} (form_id, dataset_id, value) VALUES ('%d', '%d', '')”, $formfield->id, $dataset_id ) );
}
}
if ( isset($_FILES['projectmanager_image']) && $_FILES['projectmanager_image']['name'] != '' )
$this->uploadImage($dataset_id, $_FILES['projectmanager_image']);

$this->setMessage( __( 'New dataset added to the database.', 'projectmanager' ) );

do_action('projectmanager_add_dataset', $dataset_id);
}

AnOtherWiki: A free encyclopedia by, for, and about Otherkin.  Join us, and help us grow.

 

Most Users Ever Online: 252

Currently Online:
8 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Devices in use: Phone (8)

Members Birthdays
Today: None
Upcoming: None