Skip to content

Commit

Permalink
add a bunch of hooks for setting more data on wordpress objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanstegall committed Nov 2, 2016
1 parent 913bbb6 commit 9b0534b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 19 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ Current hooks include:
- `salesforce_rest_api_upsert_custom_wordpress_item`: allow plugins to run their own upsert methods for checking for existence of, and then adding or updating, new WordPress objects, if they cannot use built in methods.
- `salesforce_rest_api_update_custom_wordpress_item`: allow plugins to run their own update methods for updating existing, mapped WordPress objects, if they cannot use built in methods.
- `salesforce_rest_api_delete_custom_wordpress_item`: allow plugins to run their own delete methods for deleting existing, mapped WordPress objects, if they cannot use built in methods.
- `salesforce_rest_api_set_more_user_data`: allow plugins to set more data on user objects when users are created or updated. This could be used for permissions, for example.
- `salesforce_rest_api_set_more_post_data`: allow plugins to set more data on post objects when posts are created or updated.
- `salesforce_rest_api_set_more_attachment_data`: allow plugins to set more data on attachment objects when attachments are created or updated.
- `salesforce_rest_api_set_more_term_data`: allow plugins to set more data on term objects when terms are created or updated.
- `salesforce_rest_api_set_more_comment_data`: allow plugins to set more data on comment objects when comments are created or updated.

### Actions

Expand Down
139 changes: 120 additions & 19 deletions classes/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,14 @@ private function user_create( $params, $id_field = 'ID' ) {
}
}

// todo: add a hook for setting permissions and other data here
// developers can use this hook to set any other user data - permissions, etc
apply_filters( 'salesforce_rest_api_set_more_user_data', array(
'user_id' => $user_id,
'params' => $params
);

// send notification of new user
// todo: make sure this respects other settings.
// ex: if admin users never get notifications, this should not force a notification
// todo: figure out what permissions out to get notifications for this and make sure it works the right way
wp_new_user_notification( $user_id, NULL, 'admin user' );

}
Expand Down Expand Up @@ -869,7 +872,19 @@ private function user_upsert( $key, $value, $methods = array(), $params, $id_fie
$result = $this->user_update( $user_id, $params );
return $result;
}
// todo: log an error here because we don't have a user id or a new user
// create log entry for lack of a user id
if ( isset( $this->logging ) ) {
$logging = $this->logging;
} else if ( class_exists( 'Salesforce_Logging' ) ) {
$logging = new Salesforce_Logging( $this->wpdb, $this->version, $this->text_domain );
}
$logging->setup(
__( 'Error: Users: Tried to run user_upsert, and ended up without a user id', $this->text_domain ),
'',
0,
0,
'error'
);

}

Expand Down Expand Up @@ -915,7 +930,11 @@ private function user_update( $user_id, $params, $id_field = 'ID' ) {
$errors[] = array( 'key' => $key, 'value' => $value );
}
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other user data - permissions, etc
apply_filters( 'salesforce_rest_api_set_more_user_data', array(
'user_id' => $user_id,
'params' => $params
);
}

$result = array( 'data' => array( $id_field => $user_id, 'success' => $success ), 'errors' => $errors );
Expand Down Expand Up @@ -990,7 +1009,11 @@ private function post_create( $params, $id_field = 'ID', $post_type = '' ) {
$errors[] = array( 'message' => __( 'Tried to upsert meta with method ' . $method . ' .' ), 'key' => $key, 'value' => $value );
}
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other post data
apply_filters( 'salesforce_rest_api_set_more_post_data', array(
'post_id' => $post_id,
'params' => $params
);
}

if ( is_wp_error( $post_id ) ) {
Expand Down Expand Up @@ -1112,7 +1135,19 @@ private function post_upsert( $key, $value, $methods = array(), $params, $id_fie
$result = $this->post_update( $post_id, $params );
return $result;
}
// todo: log an error here because we don't have a post id or a new post
// create log entry for lack of a post id
if ( isset( $this->logging ) ) {
$logging = $this->logging;
} else if ( class_exists( 'Salesforce_Logging' ) ) {
$logging = new Salesforce_Logging( $this->wpdb, $this->version, $this->text_domain );
}
$logging->setup(
__( 'Error: Posts: Tried to run post_upsert, and ended up without a post id', $this->text_domain ),
'',
0,
0,
'error'
);

}

Expand Down Expand Up @@ -1167,7 +1202,11 @@ private function post_update( $post_id, $params, $id_field = 'ID', $post_type =
$errors[] = array( 'key' => $key, 'value' => $value );
}
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other post data
apply_filters( 'salesforce_rest_api_set_more_post_data', array(
'post_id' => $post_id,
'params' => $params
);
}

$result = array( 'data' => array( $id_field => $post_id, 'success' => $success ), 'errors' => $errors );
Expand Down Expand Up @@ -1247,7 +1286,11 @@ private function attachment_create( $params, $id_field = 'ID' ) {
if ( $parent !== 0 ) {
set_post_thumbnail( $parent_post_id, $attachment_id );
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other attachment data
apply_filters( 'salesforce_rest_api_set_more_attachment_data', array(
'attachment_id' => $attachment_id,
'params' => $params
);
}

$result = array( 'data' => array( $id_field => $attachment_id, 'success' => $success ), 'errors' => $errors );
Expand Down Expand Up @@ -1353,7 +1396,20 @@ private function attachment_upsert( $key, $value, $methods = array(), $params, $
$result = $this->attachment_update( $attachment_id, $params );
return $result;
}
// todo: log an error here because we don't have an attachment id or a new attachment

// create log entry for lack of an attachment id
if ( isset( $this->logging ) ) {
$logging = $this->logging;
} else if ( class_exists( 'Salesforce_Logging' ) ) {
$logging = new Salesforce_Logging( $this->wpdb, $this->version, $this->text_domain );
}
$logging->setup(
__( 'Error: Attachment: Tried to run attachment_upsert, and ended up without an attachment id', $this->text_domain ),
'',
0,
0,
'error'
);

}

Expand Down Expand Up @@ -1387,8 +1443,6 @@ private function attachment_update( $attachment_id, $params, $id_field = 'ID' )
}
}

// todo: need hook here to take filename and parent fields here, and perhaps other fields since it is an update

if ( isset( $params['filename']['value'] ) ) {
$filename = $params['filename']['value'];
} else {
Expand Down Expand Up @@ -1442,7 +1496,12 @@ private function attachment_update( $attachment_id, $params, $id_field = 'ID' )
if ( $parent !== 0 ) {
set_post_thumbnail( $parent_post_id, $attachment_id );
}
// todo: add a hook for setting other data here

// developers can use this hook to set any other attachment data
apply_filters( 'salesforce_rest_api_set_more_attachment_data', array(
'attachment_id' => $attachment_id,
'params' => $params
);

}

Expand Down Expand Up @@ -1519,7 +1578,11 @@ private function term_create( $params, $taxonomy, $id_field = 'ID' ) {
$errors[] = array( 'message' => __( 'Tried to upsert meta with method ' . $method . ' .' ), 'key' => $key, 'value' => $value );
}
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other term data
apply_filters( 'salesforce_rest_api_set_more_term_data', array(
'term_id' => $term_id,
'params' => $params
);
}

if ( is_wp_error( $term ) ) {
Expand Down Expand Up @@ -1607,7 +1670,19 @@ private function term_upsert( $key, $value, $methods = array(), $params, $taxono
$result = $this->term_update( $term_id, $params, $taxonomy, $id_field );
return $result;
}
// todo: log an error here because we don't have a user id or a new user
// create log entry for lack of a term id
if ( isset( $this->logging ) ) {
$logging = $this->logging;
} else if ( class_exists( 'Salesforce_Logging' ) ) {
$logging = new Salesforce_Logging( $this->wpdb, $this->version, $this->text_domain );
}
$logging->setup(
__( 'Error: Terms: Tried to run term_upsert, and ended up without a term id', $this->text_domain ),
'',
0,
0,
'error'
);

}

Expand Down Expand Up @@ -1657,7 +1732,11 @@ private function term_update( $term_id, $params, $taxonomy, $id_field = 'ID' ) {
$errors[] = array( 'message' => __( 'Tried to update meta with method ' . $method . ' .' ), 'key' => $key, 'value' => $value );
}
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other term data
apply_filters( 'salesforce_rest_api_set_more_term_data', array(
'term_id' => $term_id,
'params' => $params
);
}

if ( is_wp_error( $term ) ) {
Expand Down Expand Up @@ -1735,7 +1814,11 @@ private function comment_create( $params, $id_field = 'comment_ID' ) {
$errors[] = array( 'message' => __( 'Tried to add meta with method ' . $method . ' .' ), 'key' => $key, 'value' => $value );
}
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other comment data
apply_filters( 'salesforce_rest_api_set_more_comment_data', array(
'comment_id' => $comment_id,
'params' => $params
);
}

if ( is_wp_error( $comment_id ) ) {
Expand All @@ -1744,7 +1827,6 @@ private function comment_create( $params, $id_field = 'comment_ID' ) {
} else {
$success = TRUE;
$errors = array();
// todo: add a hook for setting other data here
}

$result = array( 'data' => array( $id_field => $comment_id, 'success' => $success ), 'errors' => $errors );
Expand Down Expand Up @@ -1846,6 +1928,21 @@ private function comment_upsert( $key, $value, $methods, $params, $id_field = 'c
return $result;
}


// create log entry for lack of a comment id
if ( isset( $this->logging ) ) {
$logging = $this->logging;
} else if ( class_exists( 'Salesforce_Logging' ) ) {
$logging = new Salesforce_Logging( $this->wpdb, $this->version, $this->text_domain );
}
$logging->setup(
__( 'Error: Comments: Tried to run comment_upsert, and ended up without a comment id', $this->text_domain ),
'',
0,
0,
'error'
);

}

/**
Expand Down Expand Up @@ -1892,7 +1989,11 @@ private function comment_update( $comment_id, $params, $id_field = 'comment_ID'
$errors[] = array( 'message' => __( 'Tried to update meta with method ' . $method . ' .' ), 'key' => $key, 'value' => $value );
}
}
// todo: add a hook for setting other data here
// developers can use this hook to set any other comment data
apply_filters( 'salesforce_rest_api_set_more_comment_data', array(
'comment_id' => $comment_id,
'params' => $params
);
}

if ( is_wp_error( $updated ) ) {
Expand Down

0 comments on commit 9b0534b

Please sign in to comment.