コード例 #1
0
ファイル: taxonomy.php プロジェクト: ecerta/webcomic
 /**
  * Handle storyline sorting.
  * 
  * @hook admin_init
  * @uses WebcomicAdmin::notify()
  */
 public function admin_init()
 {
     global $wpdb;
     if (isset($_POST['webcomic_action']) and 'term_sort' === $_POST['webcomic_action'] and wp_verify_nonce($_POST['webcomic_term_sort'], 'webcomic_term_sort')) {
         if (isset($_POST['webcomic_cancel_sort'])) {
             wp_redirect(add_query_arg(array('taxonomy' => $_POST['webcomic_taxonomy'], 'post_type' => $_POST['webcomic_collection']), admin_url('edit-tags.php')));
             die;
         } else {
             $terms = $count = array();
             parse_str($_POST['webcomic_terms'], $terms);
             foreach ($terms['term'] as $k => $v) {
                 $count[$v] = empty($count[$v]) ? 1 : $count[$v] + 1;
                 $wpdb->update($wpdb->terms, array('term_group' => $count[$v]), array('term_id' => $k));
                 $wpdb->update($wpdb->term_taxonomy, array('parent' => 'null' === $v ? 0 : $v), array('term_id' => $k, 'taxonomy' => $_POST['webcomic_taxonomy']));
             }
             clean_term_cache(array_keys($terms['term']), $_POST['webcomic_taxonomy']);
             WebcomicAdmin::notify('<b>' . __('Order updated.', 'webcomic') . '</b>');
         }
     }
 }
コード例 #2
0
ファイル: admin.php プロジェクト: ecerta/webcomic
 /**
  * Activation hook.
  *
  * If no configuration exists or a legacy configuration is detected
  * we load the default configuration (saving legacy configurations
  * to 'webcomic_legacy' as necessary), schedule the
  * webcomic_buffer_alert event, and flush rewrite rules. If an
  * existing modern configuration exists but is older than the
  * internal version we run an upgrade.
  * 
  * @uses Webcomic::$config
  * @uses Webcomic::$version
  * @uses WebcomicUpgrade
  * @hook webcomic.php
  */
 public function activate()
 {
     if (!self::$config or version_compare(self::$config['version'], '4x', '<')) {
         $name = __('Untitled Webcomic', 'webcomic');
         $slug = sanitize_title($name);
         $legacy = self::$config ? self::$config : get_option('webcomic_version');
         self::$config = array('version' => self::$version, 'increment' => 2, 'thanks' => true, 'convert' => false, 'dynamic' => false, 'gestures' => false, 'integrate' => false, 'shortcuts' => false, 'uninstall' => false, 'sizes' => array(), 'terms' => array(), 'collections' => array('webcomic1' => array('id' => 'webcomic1', 'name' => $name, 'image' => 0, 'theme' => '', 'updated' => 0, 'supports' => array('title', 'editor', 'author', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions'), 'taxonomies' => array(), 'description' => '', 'feeds' => array('hook' => true, 'size' => 'thumbnail', 'main' => true), 'slugs' => array('name' => $slug, 'archive' => $slug, 'webcomic' => $slug, 'storyline' => "{$slug}-storyline", 'character' => "{$slug}-character"), 'buffer' => array('hook' => true, 'days' => 7, 'email' => get_bloginfo('admin_email')), 'access' => array('byage' => false, 'byrole' => false, 'age' => 18, 'roles' => array('!')), 'twitter' => array('media' => false, 'format' => __('%collection-name has updated! %site-url', 'webcomic'), 'oauth_token' => '', 'oauth_secret' => '', 'consumer_key' => '', 'consumer_secret' => '', 'request_token' => '', 'request_secret' => ''), 'commerce' => array('business' => '', 'currency' => 'USD', 'prints' => false, 'originals' => false, 'method' => '_xclick', 'donation' => 0, 'price' => array('domestic' => 6, 'international' => 9, 'original' => 12), 'shipping' => array('domestic' => 4, 'international' => 6, 'original' => 8), 'total' => array('domestic' => 10, 'international' => 15, 'original' => 20)), 'transcripts' => array('open' => true, 'languages' => array('!'), 'permission' => 'register', 'notify' => array('hook' => true, 'email' => get_bloginfo('admin_email'))))));
         if (is_array($legacy)) {
             self::$config['legacy'] = 3;
             $legacy_config = $legacy;
         } elseif ($legacy) {
             self::$config['legacy'] = version_compare($legacy, '2', '>=') ? 2 : 1;
             $legacy_config = array('webcomic_version' => $legacy, 'comic_category' => get_option('comic_category'), 'comic_directory' => get_option('comic_directory'), 'comic_current_chaper' => get_option('comic_current_chapter'), 'comic_secure_names' => get_option('comic_secure_names'), 'comic_feed' => get_option('comic_feed'), 'comic_feed_size' => get_option('comic_feed_size'), 'comic_large_size_w' => get_option('comic_large_size_w'), 'comic_large_size_h' => get_option('comic_large_size_h'), 'comic_medium_size_w' => get_option('comic_medium_size_w'), 'comic_medium_size_h' => get_option('comic_medium_size_h'));
             delete_option('webcomic_version');
             delete_option('comic_category');
             delete_option('comic_directory');
             delete_option('comic_current_chapter');
             delete_option('comic_secure_names');
             delete_option('comic_feed');
             delete_option('comic_feed_size');
             delete_option('comic_large_size_w');
             delete_option('comic_large_size_h');
             delete_option('comic_medium_size_w');
             delete_option('comic_medium_size_h');
             if (2 === self::$config['legacy']) {
                 $legacy_config = array_merge($legacy_config, array('comic_buffer' => get_option('comic_buffer'), 'comic_thumb_crop' => get_option('comic_thumb_crop'), 'comic_buffer_alert' => get_option('comic_buffer_alert'), 'comic_secure_paths' => get_option('comic_secure_paths'), 'comic_thumb_size_w' => get_option('comic_thumb_size_w'), 'comic_thumb_size_h' => get_option('comic_thumb_size_h'), 'comic_keyboard_shortcuts' => get_option('comic_keyboard_shortcuts'), 'comic_transcripts_allowed' => get_option('comic_transcripts_allowed'), 'comic_transcripts_required' => get_option('comic_transcripts_required'), 'comic_transcripts_loggedin' => get_option('comic_transcripts_loggedin')));
                 delete_option('comic_buffer');
                 delete_option('comic_thumb_crop');
                 delete_option('comic_buffer_alert');
                 delete_option('comic_secure_paths');
                 delete_option('comic_thumb_size_w');
                 delete_option('comic_thumb_size_h');
                 delete_option('comic_keyboard_shortcuts');
                 delete_option('comic_transcripts_allowed');
                 delete_option('comic_transcripts_required');
                 delete_option('comic_transcripts_loggedin');
             } else {
                 $legacy_config = array_merge($legacy_config, array('comic_auto_post' => get_option('comic_auto_post'), 'comic_name_format' => get_option('comic_name_format'), 'comic_thumbnail_crop' => get_option('comic_thumbnail_crop'), 'comic_name_format_date' => get_option('comic_name_format_date'), 'comic_thumbnail_size_w' => get_option('comic_thumbnail_size_w'), 'comic_thumbnail_size_h' => get_option('comic_thumbnail_size_h')));
                 delete_option('comic_auto_post');
                 delete_option('comic_name_format');
                 delete_option('comic_thumbnail_crop');
                 delete_option('comic_name_format_date');
                 delete_option('comic_thumbnail_size_w');
                 delete_option('comic_thumbnail_size_h');
             }
         } elseif ($legacy_config = get_option("comicpress-options") or $legacy_config = get_option("cp-options")) {
             global $comiccat, $comic_folder;
             self::$config["legacy"] = "ComicPress";
             $legacy_config = array_merge(array("comiccat" => $comiccat, "date_format" => defined("CP_DATE_FORMAT") ? CP_DATE_FORMAT : "Y-m-d", "comic_folder" => $comic_folder), $legacy_config);
         }
         if (isset(self::$config['legacy'])) {
             self::$config['legacy_notice'] = true;
             delete_option('webcomic_legacy');
             update_option('webcomic_legacy', $legacy_config, '', 'no');
         }
         delete_option('webcomic_options');
         add_option('webcomic_options', self::$config);
         wp_schedule_event((int) current_time('timestamp'), 'daily', 'webcomic_buffer_alert');
     } elseif (version_compare(self::$config['version'], self::$version, '<')) {
         require_once self::$dir . '-/php/upgrade.php';
         new WebcomicUpgrade();
     }
     flush_rewrite_rules();
 }
コード例 #3
0
ファイル: config.php プロジェクト: ecerta/webcomic
 /**
  * Save callback for the webcomic option.
  * 
  * If 'webcomic_general' is set we're working on the general
  * settings page.
  * 
  * If 'webcomic_collection' is set we're working on a collection
  * settings page.
  * 
  * @param array $options Configuration array.
  * @return array
  * @uses Webcomic::$config
  * @uses WebcomicAdmin::notify
  * @uses WebcomicAdmin::save_collection()
  */
 public function save($options)
 {
     $new = $bulk = false;
     $_POST = stripslashes_deep($_POST);
     if (isset($_POST['webcomic_general'])) {
         if (isset($_POST['webcomic_add_collection'])) {
             $new = self::$config['collections']['webcomic1'];
             $name = $_POST['webcomic_new_collection'] ? $_POST['webcomic_new_collection'] : sprintf(__('Untitled Webcomic %d', 'webcomic'), self::$config['increment']);
             $slug = sanitize_title($name);
             $new['id'] = 'webcomic' . self::$config['increment'];
             $new['name'] = $name;
             $new['image'] = 0;
             $new['updated'] = 0;
             $new['description'] = '';
             $new['slugs'] = array('name' => $slug, 'archive' => $slug, 'webcomic' => $slug, 'storyline' => "{$slug}-storyline", 'character' => "{$slug}-character");
             self::$config['collections']['webcomic' . self::$config['increment']] = $new;
             self::$config['increment']++;
             add_settings_error('webcomic_otions', 'new-collection', sprintf(__('Added <b>%s</b>', 'webcomic'), esc_html($name)), 'updated');
         } elseif ($_POST['webcomic_bulk_collection'] and isset($_POST['webcomic_collection'])) {
             $bulk = true;
             $count = 0;
             if ('delete' === $_POST['webcomic_bulk_collection']) {
                 foreach ($_POST['webcomic_collection'] as $id) {
                     foreach (get_posts(array('numberposts' => -1, 'post_type' => $id, 'post_status' => 'any')) as $webcomic) {
                         wp_delete_post($webcomic->ID, true);
                     }
                     foreach (get_terms("{$id}_storyline", array('get' => 'all', 'fields' => 'ids')) as $storyline) {
                         wp_delete_term($storyline, "{$id}_storyline");
                     }
                     foreach (get_terms("{$id}_character", array('get' => 'all', 'fields' => 'ids')) as $character) {
                         wp_delete_term($character, "{$id}_character");
                     }
                     delete_post_meta(self::$config['collections'][$id]['image'], '_wp_attachment_context', $id);
                     unset(self::$config['collections'][$id]);
                     $count++;
                 }
             } elseif ('delete_save' === $_POST['webcomic_bulk_collection']) {
                 foreach ($_POST['webcomic_collection'] as $id) {
                     WebcomicAdmin::save_collection($id);
                     unset(self::$config['collections'][$id]);
                     $count++;
                 }
             }
             if ('delete' === $_POST['webcomic_bulk_collection'] or 'delete_save' === $_POST['webcomic_bulk_collection']) {
                 add_settings_error('webcomic-options', 'delete-convert-collections', sprintf(_n('Deleted %s collection', 'Deleted %s collections', $count, 'webcomic'), $count), 'updated');
             }
         } else {
             self::$config['dynamic'] = isset($_POST['webcomic_dynamic']);
             self::$config['gestures'] = isset($_POST['webcomic_gestures']);
             self::$config['integrate'] = isset($_POST['webcomic_integrate']);
             self::$config['shortcuts'] = isset($_POST['webcomic_shortcuts']);
             self::$config['uninstall'] = isset($_POST['webcomic_uninstall']);
             self::$config['convert'] = isset($_POST['webcomic_uninstall'], $_POST['webcomic_convert']);
         }
     } elseif (isset($_POST['webcomic_collection'])) {
         $id = $_POST['webcomic_collection'];
         $tokens = array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%author%', "%{$id}_storyline%");
         $collection = array('id' => $id, 'name' => $_POST['webcomic_name'] ? $_POST['webcomic_name'] : self::$config['collections'][$id]['name'], 'image' => $_POST['webcomic_image'], 'theme' => $_POST['webcomic_theme'] ? $_POST['webcomic_theme'] : '', 'updated' => self::$config['collections'][$id]['updated'], 'supports' => isset($_POST['webcomic_supports']) ? array_merge($_POST['webcomic_supports'], array('author')) : array('author'), 'taxonomies' => isset($_POST['webcomic_taxonomies']) ? $_POST['webcomic_taxonomies'] : array(), 'description' => $_POST['webcomic_description'], 'feeds' => array('hook' => isset($_POST['webcomic_feeds_hook']), 'size' => $_POST['webcomic_feeds_size'], 'main' => isset($_POST['webcomic_feeds_main'])), 'slugs' => array('name' => sanitize_title($_POST['webcomic_slug'], self::$config['collections'][$id]['slugs']['name']), 'archive' => self::$config['collections'][$id]['slugs']['archive'], 'webcomic' => self::$config['collections'][$id]['slugs']['webcomic'], 'storyline' => self::$config['collections'][$id]['slugs']['storyline'], 'character' => self::$config['collections'][$id]['slugs']['character']), 'buffer' => array('hook' => isset($_POST['webcomic_buffer_hook']), 'days' => intval($_POST['webcomic_buffer_days']), 'email' => filter_var($_POST['webcomic_buffer_email'], FILTER_VALIDATE_EMAIL) ? $_POST['webcomic_buffer_email'] : self::$config['collections'][$id]['buffer']['email']), 'access' => array('byage' => isset($_POST['webcomic_access_byage']), 'byrole' => isset($_POST['webcomic_access_byrole']), 'age' => intval($_POST['webcomic_access_age']), 'roles' => array('!')), 'twitter' => array('media' => isset($_POST['webcomic_twitter_media']), 'format' => $_POST['webcomic_twitter_format'], 'oauth_token' => self::$config['collections'][$id]['twitter']['oauth_token'], 'oauth_secret' => self::$config['collections'][$id]['twitter']['oauth_secret'], 'consumer_key' => $_POST['webcomic_twitter_consumer_key'], 'consumer_secret' => $_POST['webcomic_twitter_consumer_secret'], 'request_token' => self::$config['collections'][$id]['twitter']['request_token'], 'request_secret' => self::$config['collections'][$id]['twitter']['request_secret']), 'commerce' => array('business' => filter_var($_POST['webcomic_commerce_business'], FILTER_VALIDATE_EMAIL) ? $_POST['webcomic_commerce_business'] : '', 'currency' => $_POST['webcomic_commerce_currency'], 'method' => $_POST['webcomic_commerce_method'], 'donation' => is_float(0 + $_POST['webcomic_commerce_donation']) ? round($_POST['webcomic_commerce_donation'], 2) : intval($_POST['webcomic_commerce_donation']), 'price' => array('domestic' => is_float(0 + $_POST['webcomic_commerce_prices_domestic']) ? round($_POST['webcomic_commerce_prices_domestic'], 2) : intval($_POST['webcomic_commerce_prices_domestic']), 'international' => is_float(0 + $_POST['webcomic_commerce_prices_international']) ? round($_POST['webcomic_commerce_prices_international'], 2) : intval($_POST['webcomic_commerce_prices_international']), 'original' => is_float(0 + $_POST['webcomic_commerce_prices_original']) ? round($_POST['webcomic_commerce_prices_original'], 2) : intval($_POST['webcomic_commerce_prices_original'])), 'shipping' => array('domestic' => is_float(0 + $_POST['webcomic_commerce_shipping_domestic']) ? round($_POST['webcomic_commerce_shipping_domestic'], 2) : intval($_POST['webcomic_commerce_shipping_domestic']), 'international' => is_float(0 + $_POST['webcomic_commerce_shipping_international']) ? round($_POST['webcomic_commerce_shipping_international'], 2) : intval($_POST['webcomic_commerce_shipping_international']), 'original' => is_float(0 + $_POST['webcomic_commerce_shipping_original']) ? round($_POST['webcomic_commerce_shipping_original'], 2) : intval($_POST['webcomic_commerce_shipping_original']))), 'transcripts' => array('open' => isset($_POST['webcomic_transcripts_open']), 'languages' => array('!'), 'permission' => $_POST['webcomic_transcripts_permission'], 'notify' => array('hook' => isset($_POST['webcomic_transcripts_hook']), 'email' => filter_var($_POST['webcomic_transcripts_email'], FILTER_VALIDATE_EMAIL) ? $_POST['webcomic_transcripts_email'] : self::$config['collections'][$id]['transcripts']['notify']['email'])));
         if ($_POST['webcomic_image']) {
             if (isset(self::$config['collections'][$id]['image']) and self::$config['collections'][$id]['image'] !== $_POST['webcomic_image']) {
                 delete_post_meta(self::$config['collections'][$id]['image'], '_wp_attachment_context', $id);
             }
             update_post_meta($_POST['webcomic_image'], '_wp_attachment_context', $id);
         } else {
             delete_post_meta(self::$config['collections'][$id]['image'], '_wp_attachment_context', $id);
         }
         foreach ($_POST['webcomic_slugs'] as $k => $v) {
             $slug = array();
             foreach (explode('/', $v) as $b) {
                 if ('webcomic' === $k and in_array($b, $tokens) or $b = sanitize_title($b)) {
                     $slug[] = $b;
                 }
             }
             if ($slug = implode('/', $slug)) {
                 $collection['slugs'][$k] = $slug;
             }
         }
         if ($_POST['webcomic_access_roles'] and !in_array('!', $_POST['webcomic_access_roles'])) {
             $collection['access']['roles'] = array();
             foreach ($_POST['webcomic_access_roles'] as $role) {
                 $collection['access']['roles'][] = $role;
             }
         }
         if ($collection['twitter']['consumer_key'] !== self::$config['collections'][$id]['twitter']['consumer_key'] or $collection['twitter']['consumer_secret'] !== self::$config['collections'][$id]['twitter']['consumer_secret']) {
             $collection['twitter']['oauth_token'] = $collection['twitter']['oauth_secret'] = '';
         } elseif ($collection['twitter']['oauth_token'] and $collection['twitter']['oauth_secret']) {
             $collection['twitter']['request_token'] = $collection['twitter']['request_secret'] = '';
         }
         $collection['commerce']['prints'] = (isset($_POST['webcomic_commerce_prints']) and $collection['commerce']['business']);
         $collection['commerce']['originals'] = (isset($_POST['webcomic_commerce_originals']) and $collection['commerce']['business']);
         $collection['commerce']['total']['domestic'] = round($collection['commerce']['price']['domestic'] + $collection['commerce']['shipping']['domestic'], 2);
         $collection['commerce']['total']['international'] = round($collection['commerce']['price']['international'] + $collection['commerce']['shipping']['international'], 2);
         $collection['commerce']['total']['original'] = round($collection['commerce']['price']['original'] + $collection['commerce']['shipping']['original'], 2);
         if (!empty($_POST['webcomic_transcripts_languages']) and !in_array('!', $_POST['webcomic_transcripts_languages'])) {
             $collection['transcripts']['languages'] = array();
             foreach ($_POST['webcomic_transcripts_languages'] as $language) {
                 $collection['transcripts']['languages'][] = $language;
             }
         }
         self::$config['collections'][$id] = $collection;
         WebcomicAdmin::notify('<b>' . __('Settings saved.', 'webcomic') . '</b>');
     }
     return (isset($_POST['webcomic_general']) or isset($_POST['webcomic_collection'])) ? self::$config : $options;
 }
コード例 #4
0
ファイル: media.php プロジェクト: ecerta/webcomic
 /**
  * Handle media functions.
  * 
  * @uses Webcomic::$config
  * @uses WebcomicAdmin::notify()
  * @hook admin_init
  */
 public function admin_init()
 {
     global $wpdb;
     if (isset($_GET['webcomic_action'], $_GET['post']) and 'regenerate' === $_GET['webcomic_action'] and $meta = wp_get_attachment_metadata($_GET['post'])) {
         check_admin_referer('webcomic_regenerate');
         $dir = wp_upload_dir();
         foreach (get_intermediate_image_sizes() as $size) {
             if ($file = image_get_intermediate_size($_GET['post'], $size)) {
                 @unlink(path_join($dir['basedir'], $file['path']));
             }
         }
         if (is_wp_error($meta = wp_generate_attachment_metadata($_GET['post'], get_attached_file($_GET['post']))) and empty($_GET['webcomic_hide_notice'])) {
             WebcomicAdmin::notify(__('Images could not be regenerated.', "webcomic"), 'error');
         } elseif (empty($_GET['webcomic_hide_notice'])) {
             wp_update_attachment_metadata($_GET['post'], $meta);
             WebcomicAdmin::notify(__('Regenerated images for media attachments.', "webcomic"));
         }
     }
     if (isset($_GET['webcomic_action'], $_GET['post']) and 'detach' === $_GET['webcomic_action'] and intval($_GET['post'])) {
         check_admin_referer('webcomic_detach');
         if (wp_update_post(array('ID' => (int) $_GET['post'], 'post_parent' => 0)) and empty($_GET['webcomic_hide_notice'])) {
             WebcomicAdmin::notify(__('Detached media.', "webcomic"));
         } elseif (empty($_GET['webcomic_hide_notice'])) {
             WebcomicAdmin::notify(__('Media could not be detached.', "webcomic"), 'error');
         }
     }
     if (isset($_POST['webcomic_action']) and 'webcomic_regenerate' === $_POST['webcomic_action'] and !empty($_POST['media'])) {
         check_admin_referer('bulk-media');
         $dir = wp_upload_dir();
         $good = $bad = 0;
         $sizes = get_intermediate_image_sizes();
         foreach ($_POST['media'] as $id) {
             if (false !== strpos(get_post_mime_type($id), 'image')) {
                 foreach ($sizes as $size) {
                     if ($file = image_get_intermediate_size($id, $size)) {
                         @unlink(path_join($dir['basedir'], $file['path']));
                     }
                 }
                 if (is_wp_error($meta = wp_generate_attachment_metadata($id, get_attached_file($id)))) {
                     $bad++;
                 } else {
                     wp_update_attachment_metadata($id, $meta);
                     $good++;
                 }
             }
         }
         if ($bad) {
             WebcomicAdmin::notify(sprintf(_n('Images could not be regenerated for %s media attachment.', 'Images could not be regenerated for %s media attachments.', $bad, "webcomic"), $bad), 'error');
         }
         if ($good) {
             WebcomicAdmin::notify(sprintf(_n('Images regenerated for %s media attachment.', 'Images regenerated for %s media attachments.', $good, "webcomic"), $good));
         }
     }
     if (isset($_POST['webcomic_action']) and 'webcomic_detach' === $_POST['webcomic_action'] and !empty($_POST['media'])) {
         check_admin_referer('bulk-media');
         $good = $bad = 0;
         foreach ($_POST['media'] as $id) {
             if ($wpdb->get_var($wpdb->prepare("SELECT post_parent FROM {$wpdb->posts} WHERE ID = %s", $id))) {
                 if (wp_update_post(array('ID' => (int) $id, 'post_parent' => 0))) {
                     $good++;
                 } else {
                     $bad++;
                 }
             }
         }
         if ($bad) {
             WebcomicAdmin::notify(sprintf(_n('Could not detach %s attachment.', 'Could not detach %s attachments.', $bad, "webcomic"), $bad), 'error');
         }
         if ($good) {
             WebcomicAdmin::notify(sprintf(_n('Detached %s attachment.', 'Detached %s attachments.', $good, "webcomic"), $good));
         }
     }
     if (isset($_POST['webcomic_action']) and 'attach' === $_POST['webcomic_action'] and wp_verify_nonce($_POST['webcomic_attach'], 'webcomic_attach')) {
         if (empty($_POST['matches'])) {
             WebcomicAdmin::notify(__('No matches selected.', "webcomic"), 'error');
         } else {
             $good = $bad = 0;
             foreach ($_POST['matches'] as $match) {
                 $ids = explode('|', $match);
                 if (wp_update_post(array('ID' => (int) $ids[0], 'post_parent' => (int) $ids[1]))) {
                     $good++;
                 } else {
                     $bad++;
                 }
             }
             if ($bad) {
                 WebcomicAdmin::notify(sprintf(_n('A media could not be attached to %s post.', 'Media could not be attached to %s posts.', $bad, "webcomic"), $bad), 'error');
             }
             WebcomicAdmin::notify(sprintf(_n('Media attached to %s post.', 'Media attached to %s posts.', $good, "webcomic"), $good));
         }
     }
     if (isset($_POST['webcomic_action']) and 'generate' === $_POST['webcomic_action'] and wp_verify_nonce($_POST['webcomic_generate'], 'webcomic_generate')) {
         if (empty($_POST['attachments']) or !($attachments = $_POST['attachments'])) {
             WebcomicAdmin::notify('<b>' . __('No media selected.', "webcomic") . '</b>', 'error');
         } elseif (!($start = strtotime($_POST['webcomic_generate_start']))) {
             WebcomicAdmin::notify('<b>' . __('The start date could not be understood.', "webcomic") . '</b>', 'error');
         } elseif (!($days = $_POST['webcomic_generate_days'])) {
             WebcomicAdmin::notify('<b>' . __('No weekdays selected.', "webcomic") . '</b>', 'error');
         } else {
             $now = $start;
             $good = $bad = 0;
             $draft = isset($_POST['webcomic_generate_draft']);
             $weekday = date('N', $start);
             $weekdays = array(1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday');
             foreach ($attachments as $id) {
                 $attachment = get_post($id);
                 if ($new_post = wp_insert_post(array('post_type' => $_POST['webcomic_collection'], 'post_date' => date('Y-m-d H:i:s', $now), 'post_title' => $attachment->post_title, 'post_status' => $draft ? 'draft' : 'publish', 'post_content' => '&nbsp;', 'post_date_gmt' => get_gmt_from_date(date('Y-m-d H:i:s', $now)))) and !is_wp_error($new_post)) {
                     if (wp_update_post(array('ID' => $id, 'post_parent' => $new_post))) {
                         add_post_meta($new_post, 'webcomic_prints', self::$config['collections'][$_POST['webcomic_collection']]['commerce']['prints'], true);
                         add_post_meta($new_post, 'webcomic_original', true, true);
                         add_post_meta($new_post, 'webcomic_transcripts', self::$config['collections'][$_POST['webcomic_collection']]['transcripts']['open'], true);
                         $good++;
                     } else {
                         $bad++;
                     }
                 } else {
                     $bad++;
                 }
                 if (false !== ($key = array_search($weekday, $days))) {
                     $weekday = empty($days[$key + 1]) ? $days[0] : $days[$key + 1];
                 } elseif ($weekday > max($days) or $weekday < min($days)) {
                     $weekday = $days[0];
                 } else {
                     foreach ($days as $day) {
                         if ($weekday < $day) {
                             $weekday = $day;
                             break;
                         }
                     }
                 }
                 $now = strtotime("next {$weekdays[$weekday]}", $now);
             }
             if ($bad) {
                 WebcomicAdmin::notify(sprintf(_n('A post could not be generated for %d media.', 'Posts could not be generated for %d media.', $bad, "webcomic"), $bad), 'error');
             }
             WebcomicAdmin::notify(sprintf(_n('Post created for %d media.', 'Posts created for %d media.', $good, "webcomic"), $good));
         }
     }
 }
コード例 #5
0
ファイル: webcomic.php プロジェクト: ecerta/webcomic
 /**
  * Auto tweet on webcomic publish.
  * 
  * @param string $new New post status.
  * @param string $old Old post status.
  * @param string $post Post object to update.
  * @uses Webcomic::$config
  * @hook transition_post_status
  * @filter string webcomic_tweet Filters the tweet text pushed to Twitter whenever a webcomic is published. Defaults to the collection-specific tweet format.
  */
 public function tweet_webcomic($new, $old, $post)
 {
     if ('publish' === $new and 'publish' !== $old and !empty(self::$config['collections'][$post->post_type]['twitter']['format']) and !empty(self::$config['collections'][$post->post_type]['twitter']['oauth_token']) and !empty(self::$config['collections'][$post->post_type]['twitter']['oauth_secret'])) {
         $status = self::$config['collections'][$post->post_type]['twitter']['format'];
         $attachments = self::get_attachments($post->ID);
         $attachment = current($attachments);
         if (false !== strpos(self::$config['collections'][$post->post_type]['twitter']['format'], '%')) {
             $link = wp_get_shortlink($post->ID);
             $tokens = array_merge(array('%url' => $link ? $link : get_permalink($post->ID), '%date' => get_the_time(get_option('date_format'), $post), '%time' => get_the_time(get_option('time_format'), $post), '%title' => html_entity_decode(get_the_title($post->ID), ENT_QUOTES, "UTF-8"), '%author' => get_the_author_meta('display_name', $post->post_author), '%site-url' => home_url(), '%permalink' => get_permalink($post->ID), '%site-name' => get_bloginfo('name'), '%collection-names' => self::$config['collections'][$post->post_type]['name'], '%collection-name' => self::$config['collections'][$post->post_type]['name'], '%collections' => '#' . str_replace(array('_', '-'), '', self::$config['collections'][$post->post_type]['slugs']['name']), '%collection' => '#' . str_replace(array('_', '-'), '', self::$config['collections'][$post->post_type]['slugs']['name'])));
             foreach (array_merge(get_intermediate_image_sizes(), array('full')) as $size) {
                 $tokens["%{$size}"] = (false !== strpos($status, "%{$size}") and $image = wp_get_attachment_image_src($attachment->ID, $size)) ? $image[0] : '';
             }
             if (preg_match('/%' . implode('|%', array_merge(array('storyline', 'character'), self::$config['collections'][$post->post_type]['taxonomies'])) . '/', self::$config['collections'][$post->post_type]['twitter']['format']) and $terms = wp_get_object_terms($post->ID, array_merge(array("{$post->post_type}_storyline", "{$post->post_type}_character"), self::$config['collections'][$post->post_type]['taxonomies'])) and !is_wp_error($terms)) {
                 foreach ($terms as $term) {
                     $hash = str_replace(array('_', '-'), '', "#{$term->slug}");
                     if (preg_match('/^webcomic\\d+_(storyline|character)$/', $term->taxonomy)) {
                         $parts = explode('_', $term->taxonomy);
                         if (false === strpos($tokens['collection-names'], self::$config['collections'][$parts[0]]['name'])) {
                             $tokens['%collections'] .= ' #' . str_replace(array('_', '-'), '', self::$config['collections'][$parts[0]]['slugs']['name']);
                             $tokens['%collection-names'] .= ' ' . self::$config['collections'][$parts[0]]['name'];
                             $tokens['%xcollections'] .= empty($tokens['%xcollection-names']) ? '#' . str_replace(array('_', '-'), '', self::$config['collections'][$parts[0]]['slugs']['name']) : ' #' . str_replace(array('_', '-'), '', self::$config['collections'][$parts[0]]['slugs']['name']);
                             $tokens['%xcollection-names'] .= empty($tokens['%xcollection-names']) ? self::$config['collections'][$parts[0]]['name'] : ' ' . self::$config['collections'][$parts[0]]['name'];
                         }
                         $tokens["%{$parts[1]}s"] .= empty($tokens["%{$parts[1]}s"]) ? "{$hash}" : " {$hash}";
                         $tokens["%{$parts[1]}-names"] .= empty($tokens["%{$parts[1]}-names"]) ? "{$term->name}" : " {$term->name}";
                         if (0 === strpos($term->taxonomy, $post->post_type)) {
                             $tokens["%!{$parts[1]}s"] .= empty($tokens["%!{$parts[1]}s"]) ? "{$hash}" : " {$hash}";
                             $tokens["%!{$parts[1]}-names"] .= empty($tokens["%!{$parts[1]}-names"]) ? "{$term->name}" : " {$term->name}";
                         } else {
                             $tokens["%x{$parts[1]}s"] .= empty($tokens["%x{$parts[1]}s"]) ? "{$hash}" : " {$hash}";
                             $tokens["%x{$parts[1]}-names"] .= empty($tokens["%x{$parts[1]}-names"]) ? "{$term->name}" : " {$term->name}";
                         }
                     } else {
                         $tokens["%{$term->taxonomy}-names"] = isset($tokens["%{$term->taxonomy}-names"]) ? $tokens["%{$term->taxonomy}-names"] . " {$term->name}" : "{$term->name}";
                         $tokens["%{$term->taxonomy}"] = isset($tokens["%{$term->taxonomy}"]) ? $tokens["%{$term->taxonomy}"] . " {$hash}" : "{$hash}";
                     }
                 }
             }
             if (preg_match_all('/%field:\\{.+?\\}/', self::$config['collections'][$post->post_type]['twitter']['format'], $matches)) {
                 foreach ($matches[0] as $match) {
                     if (empty($tokens[$match])) {
                         $tokens[$match] = get_post_meta($post->ID, str_replace(array('%field:{', '}'), '', $match), $match, true);
                     }
                 }
             }
             $status = apply_filters('webcomic_tweet', str_replace(array_keys($tokens), $tokens, $status), $post);
         }
         if ($status) {
             if (!class_exists('tmhOAuth')) {
                 require_once self::$dir . '-/lib/twitter.php';
             }
             $oauth = new tmhOAuth(array('consumer_key' => self::$config['collections'][$post->post_type]['twitter']['consumer_key'], 'consumer_secret' => self::$config['collections'][$post->post_type]['twitter']['consumer_secret'], 'user_token' => self::$config['collections'][$post->post_type]['twitter']['oauth_token'], 'user_secret' => self::$config['collections'][$post->post_type]['twitter']['oauth_secret']));
             if (self::$config['collections'][$post->post_type]['twitter']['media'] and $attachment) {
                 $file = get_attached_file($attachment->ID);
                 $name = basename($file);
                 $code = $oauth->request('POST', $oauth->url('1.1/statuses/update_with_media'), array('media[]' => "@{$file};type={$attachment->post_mime_type};filename={$name}", 'status' => substr(strip_tags($status), 0, 140)), true, true);
             } else {
                 $code = $oauth->request('POST', $oauth->url('1.1/statuses/update'), array('status' => substr(strip_tags($status), 0, 140)));
             }
             if (200 !== intval($code)) {
                 $errors = get_transient('webcomic_error');
                 WebcomicAdmin::notify(sprintf(__('<b>Twitter Error:</b> %s', 'webcomic'), $oauth->response['response'] ? $oauth->response['response'] : $oauth->response['error']), 'error');
             }
         }
     }
 }