I have some functions restricted in some hooks. The purpose of these hooks was to have consistent author name in the DB(which is a product attribute) and also update the display name. I placed this in functions.php and created hooks for these functions which I thought of implementing/triggering these hooks through WP-Crontrol plugin, apparently these functions were causing CPU Overloading (when checked in AWS instance). And I'm confused why the CPU is overloading due to these hooks?
function replace_authors_name_attributes()
{
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$products = new WP_Query($args);
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
$product_id = get_the_ID();
echo "Product ID: " . $product_id . "\n";
$product_attributes = get_post_meta($product_id, '_product_attributes', true);
if (!empty($product_attributes)) {
$attributes = maybe_unserialize($product_attributes);
if (is_array($attributes) && isset($attributes['authors_name']) && isset($attributes['authors_name']['value'])) {
$authors_name = $attributes['authors_name']['value'];
echo "Author's Name: " . $authors_name . "\n<br>";
if ($authors_name === 'Ram' || $authors_name === 'Ramu') {
$new_author_name = 'Ram Rao';
echo "Replacing with: " . $new_author_name . "\n<br>";
$attributes['authors_name']['value'] = $new_author_name;
update_post_meta($product_id, '_product_attributes', $attributes);
echo "Attribute updated.\n<br>";
}
}
}
}
}
}
function schedule_authors_consistency() {
if (!wp_next_scheduled('consistent_author')) {
wp_schedule_single_event(time(), 'consistent_author');
}
}
add_action('init', 'schedule_authors_consistency');
add_action('consistent_author', 'replace_authors_name_attributes');
function update_user_display_name($user_id, $new_display_name)
{
global $wpdb;
$wpdb->update(
$wpdb->users,
array('display_name' => $new_display_name),
array('ID' => $user_id),
array('%s'),
array('%d')
);
}
function update_authors_display_name() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$products = new WP_Query($args);
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
$product_id = get_the_ID();
echo "Product ID: " . $product_id . "\n";
$author_id = get_post_field('post_author', $product_id);
if ($author_id) {
$current_display_name = get_the_author_meta('display_name', $author_id);
echo "Current Display Name: " . $current_display_name . "\n<br>";
if ($current_display_name === 'Ram') {
$new_display_name = 'Ram Rao';
echo "Replacing with: " . $new_display_name . "\n<br>";
update_user_display_name($author_id, $new_display_name);
echo "Display Name updated.\n<br>";
}
}
}
} else {
echo "No products found.\n";
}
}
wp_reset_postdata();
function schedule_authors_display_name_assignment() {
if (!wp_next_scheduled('update_authors_display_name_event')) {
wp_schedule_single_event(time(), 'update_authors_display_name_event');
}
}
add_action('init', 'schedule_authors_display_name_assignment');
add_action('update_authors_display_name_event', 'update_authors_display_name');
function get_user_id_from_authors_name($authors_name)
{
global $wpdb;
$user_id = $wpdb->get_var(
$wpdb->prepare("SELECT ID FROM $wpdb->users WHERE display_name = %s", $authors_name)
);
return $user_id;
}
function update_post_authors() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$products = new WP_Query($args);
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
$product_id = get_the_ID();
echo "Product ID: " . $product_id . "\n";
$product_attributes = get_post_meta($product_id, '_product_attributes', true);
if (!empty($product_attributes)) {
$attributes = maybe_unserialize($product_attributes);
if (is_array($attributes) && isset($attributes['authors_name']) && isset($attributes['authors_name']['value'])) {
$authors_name = $attributes['authors_name']['value'];
echo "Author's Name: " . $authors_name . "\n<br>";
$user_id = get_user_id_from_authors_name($authors_name);
if ($user_id) {
echo "User ID: " . $user_id . "\n<br>";
wp_update_post(array('ID' => $product_id, 'post_author' => $user_id));
echo "Post Author updated.\n<br>";
} else {
echo "User ID not found for the given author's name.\n<br>";
}
}
}
}
} else {
echo "No products found.\n";
}
}
wp_reset_postdata();
function schedule_update_post_authors_event() {
if (!wp_next_scheduled('update_post_authors_event')) {
wp_schedule_single_event(time(), 'update_post_authors_event');
}
}
add_action('init', 'schedule_update_post_authors_event');
add_action('update_post_authors_event', 'update_post_authors');
I know that the function which updates the display_name is a mistake, instead of iterating through all the products, I should have just updated the wp_users table.
These hooks were not triggered or executed from wp-crontrol. Why is the CPU overloading? If I know the possible reason. I may prevent this from happening in the future. please do shoot me any error in my question!! Thanks
These hooks weren't even executed and CPU is overloading by their presence. Not sure why its happening?