public function GenerateMappingsUI($lists, $groups, $templates, $javaScript) { print '<p><strong>Since you are using Events Manager</strong>, you can create campaigns based on Event categories as well. <em>If you use a \'user template\', be sure that the template\'s content section is called \'main\' so that your post\'s content can be substituted in the template.</em></p><p>' . PHP_EOL; print '<table id="event_manager_table">' . PHP_EOL; print '<tr><th>Category</th><th></th><th>Mailing List</th><th></th><th>Interest Group</th><th></th><th>User Template</th><th></th></tr>'; // Will need a list of the EM categories (terms) that the user has created. $categories = get_terms(EM_TAXONOMY_CATEGORY, 'orderby=count&hide_empty=0'); $categories = AC_AssembleTermsArray($categories); // Building array that contains the mappings. Each entry is a row in the UI. $mappings = array(); // Global DB object global $wpdb; // Pull all of the mappings from the DB. Each row will have three items. The // category is encoded in the option_name of each of the three along with an // index. $options_table_name = $wpdb->prefix . 'options'; $sql = "SELECT option_name,option_value FROM {$options_table_name} WHERE option_name like '" . EVENTS_MANAGER_MAPPING_PREFIX . "%' ORDER BY option_name"; $fields = $wpdb->get_results($sql, ARRAY_A); if ($fields) { foreach ($fields as $field) { // Split the results into an array which contains info about this mapping $info = explode('_', $field['option_name']); // Create a new array for each new index (at the 3rd element of the split // string. if (!isset($mappings[$info[3]])) { $mappings[$info[3]] = array(); } // Push this item into the array. array_push($mappings[$info[3]], $field['option_value']); } } // Now loop through the constructed array and generate a new row for each // mapping found. $highestMapping = -1; foreach ($mappings as $index => $mapping) { // The category is contained in the 1st element of the returned array. // The index is in the 0th. $newRow = AC_GenerateCategoryMappingRow($index, EVENTS_MANAGER_MAPPING_PREFIX, $categories, $mapping[0], $lists, $mapping[2], $javaScript, $groups, $mapping[1], $templates, $mapping[3]); // "template" is fourth if ($index > $highestMapping) { $highestMapping = $index; } print $newRow; } // Close out the table. print '</table>' . PHP_EOL; // Generate the javascript that lets users create new mapping rows. $nrScript = AC_GenerateNewRowScript($highestMapping + 1, "'" . EVENTS_MANAGER_MAPPING_PREFIX . "'", "'#event_manager_table'", $categories, WP88_ANY, $lists, WP88_NONE, $groups, WP88_ANY, $templates, WP88_NONE); // Add in the "new row" script. Clicking on this executes the javascript to // create a new row to map categories, lists, groups, and templates. print '<p><a href="#" id="addNewEMRow" onclick="' . $nrScript . '">Add new Events Manager post category mapping</a></p>' . PHP_EOL; print '</p>'; }
?> <div id="campaigns" class="postbox" style="width:<?php echo $uiWidth; ?> px"> <h3 class='hndle'><span>Mail Campaigns from Posts</span></h3> <div class="inside"> <p><strong>Choose how you'd like to create campaigns from your post categories.</strong> <em>If you use a 'user template', be sure that the template's content section is called 'main' so that your post's content can be substituted in the template.</em></p> <p> <table id="wp_category_mappings_table"> <tr><th>Category</th><th>Mailing List</th><th></th><th>Interest Group</th><th></th><th>User Template</th><th></th></tr> <?php // Fetch this site's categories $categories = get_categories('hide_empty=0&orderby=name'); $categories = AC_AssembleTermsArray($categories); // Building array that contains the mappings. Each entry is a row in the UI. $mappings = array(); // Need to query data in the BuddyPress extended profile table global $wpdb; // Pull all of the mappings from the DB. Each row will have three items. The // category is encoded in the option_name of each of the three along with an // index. $options_table_name = $wpdb->prefix . 'options'; $sql = "SELECT option_name,option_value FROM {$options_table_name} WHERE option_name like '" . WP88_CATEGORY_MAPPING_PREFIX . "%' ORDER BY option_name"; $fields = $wpdb->get_results($sql, ARRAY_A); if ($fields) { foreach ($fields as $field) { // Split the results into an array which contains info about this mapping $info = explode('_', $field['option_name']); // Create a new array for each new index (at the 3rd element of the split
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. } } } } }