function AC_OnPublishPost($postID) { // Get the info on this post $post = get_post($postID); $categories = AC_AssembleTermsArray(get_the_category($postID)); // Potentially several categories // Need to have the "Any" category for the big foreach below to work. Add it // here. $categories['Any'] = 'any'; if (empty($categories)) { AC_Log("There is no standard category for post {$postID}. Searching for a third-party plugin category."); // Now, search for custom categories (actually taxonomy terms) in the // various Publish plugins. $publishPlugins = new ACPublishPlugins(); $pluginCollection = $publishPlugins->GetPluginClasses($publishPlugins->GetType()); foreach ($pluginCollection as $plugin) { if ($plugin::GetInstalled() && $plugin::GetUsePlugin()) { $newTerms = AC_AssembleTermsArray($plugin::GetTerms($postID)); foreach ($newTerms as $name => $slum) { $categories[$name] = $slum; } } // If a category was found, break out. if (!empty($categories)) { AC_Log("Found additional categories through the plugin {$plugin}."); } } } AC_Log("Attempting to create a campaign for post ID {$postID}."); if (!empty($categories)) { AC_Log($categories); } // If it matches the user's category choice or is "Any" category, then // do the work. This needs to be a loop because a post can belong to // multiple categories. global $wpdb; foreach ($categories as $categoryName => $categorySlug) { // Do a SQL lookup of all category rows that match this category slug. NOTE that // the option for the "Any" category is in this SQL string. $options_table_name = $wpdb->prefix . 'options'; $sql = "SELECT option_name,option_value FROM {$options_table_name} WHERE option_name LIKE 'wp88_mc_%' AND (option_value = '{$categorySlug}' OR option_value = '" . WP88_ANY . "')"; $fields = $wpdb->get_results($sql); if ($fields) { foreach ($fields as $field) { // NOTE: This approach currently does have the problem that if a category // and a plugin's term have the same slug, then campaigns could go to the // wrong place. This is fairly unlikely, but this leak needs to be plugged // with an improved architecture here. // // This can happen because the above SQL statement does not discriminate // between categories or terms. The prefix part of the string and the index // can easily differ while the category slug is the same. // Split the results into an array which contains info about this mapping $info = explode('_', $field->option_name); // The last part of $info should contain the word "category". It's possible // that other rows will be picked up (like when the option value is "Any", // the "group" option will be picked up too since it can have an "Any" value) // so skip those here. if (0 !== strcmp($info[4], 'category')) { continue; } // Yank off the "category" from the tail of each string and replace it with the // other values, then query them. $categoryMailingList = get_option(str_replace(WP88_CATEGORY_SUFFIX, WP88_LIST_SUFFIX, $field->option_name)); $categoryGroupName = get_option(str_replace(WP88_CATEGORY_SUFFIX, WP88_GROUP_SUFFIX, $field->option_name)); $categoryTemplateID = get_option(str_replace(WP88_CATEGORY_SUFFIX, WP88_TEMPLATE_SUFFIX, $field->option_name)); AC_Log("For the {$categorySlug} category: The mailing list is: {$categoryMailingList}. The group is: {$categoryGroupName}. The template ID is: {$categoryTemplateID}."); // If the mailing list is NOT "None" then create a campaign. if (0 != strcmp($categoryMailingList, WP88_NONE)) { // Create an instance of the MailChimp API $apiKey = get_option(WP88_MC_APIKEY); $api = new MCAPI_13($apiKey); // Do the work $id = AC_CreateCampaignFromPost($api, $postID, $categoryMailingList, $categoryGroupName, $categoryTemplateID); AC_Log("Created a campaign with ID {$id} in category {$categoryName}."); // Does the user want to send the campaigns right away? $sendNow = get_option(WP88_MC_SEND_NOW); // Send it, if necessary (if user wants it), and the $id is // sufficiently long (just picking longer than 3 for fun). if ('1' == $sendNow && strlen($id) > 3) { $api->campaignSendNow($id); } // Not breaking anymore. Now, if you assign multiple categories to // create campaigns, then each will be created. } } } } }