prefix . 'aieo_dmm_campaigns'; } public static function touch($slug, $channel_hint = null) { $slug = trim((string) $slug); if ($slug === '') { return null; } $slug = mb_substr($slug, 0, 120); if (strpos($slug, '{{') !== false || strpos($slug, '}}') !== false) { return null; } if (strpos($slug, 'aieo_internal_') === 0) { return null; } $literal_placeholders = array( 'campaign_name', 'campaign.name', '{campaign_name}', '[campaign_name]', '{campaign.name}', '[campaign.name]', ); if (in_array(strtolower($slug), $literal_placeholders, true)) { return null; } if (self::$slug_to_id_cache !== null && isset(self::$slug_to_id_cache[$slug])) { return (int) self::$slug_to_id_cache[$slug]; } global $wpdb; $tbl = self::table(); $cached = wp_cache_get('aieo_camp:' . $slug, self::CACHE_GROUP); if (is_numeric($cached) && (int) $cached > 0) { $live = (int) $wpdb->get_var($wpdb->prepare( "SELECT id FROM {$tbl} WHERE id = %d AND slug = %s LIMIT 1", (int) $cached, $slug )); if ($live > 0) { self::$slug_to_id_cache[$slug] = $live; return $live; } wp_cache_delete('aieo_camp:' . $slug, self::CACHE_GROUP); } $id = (int) $wpdb->get_var($wpdb->prepare( "SELECT id FROM {$tbl} WHERE slug = %s LIMIT 1", $slug )); if ($id > 0) { wp_cache_set('aieo_camp:' . $slug, $id, self::CACHE_GROUP, self::CACHE_TTL); self::$slug_to_id_cache[$slug] = $id; return $id; } $now = current_time('mysql', true); $hint = $channel_hint ? mb_substr((string) $channel_hint, 0, 40) : null; $wpdb->insert($tbl, array( 'slug' => $slug, 'label' => $slug, 'channel_hint' => $hint, 'status' => 'auto_draft', 'created_at' => $now, 'updated_at' => $now, )); $id = (int) $wpdb->insert_id; if ($id > 0) { wp_cache_set('aieo_camp:' . $slug, $id, self::CACHE_GROUP, self::CACHE_TTL); self::$slug_to_id_cache[$slug] = $id; } return $id ?: null; } public static function id_for_slug($slug) { $slug = trim((string) $slug); if ($slug === '') { return null; } if (self::$slug_to_id_cache !== null && isset(self::$slug_to_id_cache[$slug])) { return (int) self::$slug_to_id_cache[$slug]; } global $wpdb; $id = (int) $wpdb->get_var($wpdb->prepare( "SELECT id FROM " . self::table() . " WHERE slug = %s LIMIT 1", $slug )); if ($id > 0) { self::$slug_to_id_cache[$slug] = $id; } return $id ?: null; } public static function get($id) { $id = (int) $id; if ($id <= 0) { return null; } global $wpdb; $row = $wpdb->get_row($wpdb->prepare( "SELECT * FROM " . self::table() . " WHERE id = %d LIMIT 1", $id ), ARRAY_A); return is_array($row) ? $row : null; } public static function save($id, array $patch) { global $wpdb; $id = (int) $id; if ($id <= 0) { return false; } $clean = array(); if (array_key_exists('label', $patch)) { $clean['label'] = sanitize_text_field((string) $patch['label']); } if (array_key_exists('channel_hint', $patch)) { $clean['channel_hint'] = $patch['channel_hint'] !== '' ? sanitize_key((string) $patch['channel_hint']) : null; } if (array_key_exists('objective', $patch)) { $clean['objective'] = $patch['objective'] !== '' ? sanitize_key((string) $patch['objective']) : null; } if (array_key_exists('status', $patch)) { $allowed = array('auto_draft','draft','live','paused','archived'); $s = sanitize_key((string) $patch['status']); $clean['status'] = in_array($s, $allowed, true) ? $s : 'live'; } if (array_key_exists('start_at', $patch)) { $clean['start_at'] = $patch['start_at'] ?: null; } if (array_key_exists('end_at', $patch)) { $clean['end_at'] = $patch['end_at'] ?: null; } if (array_key_exists('budget_planned', $patch)) { $clean['budget_planned'] = is_numeric($patch['budget_planned']) ? (float) $patch['budget_planned'] : null; } if (array_key_exists('budget_spent', $patch)) { $clean['budget_spent'] = is_numeric($patch['budget_spent']) ? (float) $patch['budget_spent'] : null; } if (array_key_exists('notes', $patch)) { $clean['notes'] = wp_kses_post((string) $patch['notes']); } if (self::has_offer_columns()) { if (array_key_exists('goal', $patch)) { $clean['goal'] = in_array((string) $patch['goal'], self::GOALS, true) ? (string) $patch['goal'] : ''; } if (array_key_exists('budget', $patch)) { $clean['budget'] = is_numeric($patch['budget']) && (float) $patch['budget'] >= 0 ? round((float) $patch['budget'], 4) : null; } } if (empty($clean)) { return true; } $wpdb->update(self::table(), $clean, array('id' => $id)); $slug = (string) $wpdb->get_var($wpdb->prepare( "SELECT slug FROM " . self::table() . " WHERE id = %d", $id )); if ($slug !== '') { wp_cache_delete('aieo_camp:' . $slug, self::CACHE_GROUP); if (self::$slug_to_id_cache !== null) { unset(self::$slug_to_id_cache[$slug]); } } return true; } public static function has_offer_columns() { return class_exists('AIEO_DMM_Offer_Access') && (int) get_option(AIEO_DMM_Offer_Access::SCHEMA_OPTION, 0) >= AIEO_DMM_Offer_Access::SCHEMA_VERSION; } public static function create($label, array $fields = array()) { global $wpdb; $label = sanitize_text_field((string) $label); if ('' === $label) { return new WP_Error('invalid_campaign', __('A new campaign needs a name.', 'ai-eshop-optimizer'), array('field' => 'campaign_new')); } $label = mb_substr($label, 0, 190); $base = mb_substr(sanitize_title($label), 0, 110); if ('' === $base) { $base = 'campaign'; } $slug = $base; for ($n = 2; null !== self::id_for_slug($slug); $n++) { $slug = $base . '-' . $n; } $now = current_time('mysql', true); $row = array( 'slug' => $slug, 'label' => $label, 'status' => 'draft', 'created_at' => $now, 'updated_at' => $now, ); if (false === $wpdb->insert(self::table(), $row) || (int) $wpdb->insert_id <= 0) { return new WP_Error('write_failed', 'The campaign could not be written: ' . $wpdb->last_error); } $id = (int) $wpdb->insert_id; self::$slug_to_id_cache[$slug] = $id; if (array() !== array_intersect_key($fields, array_flip(array('goal', 'budget')))) { self::save($id, array_intersect_key($fields, array_flip(array('goal', 'budget')))); } return $id; } public static function library($limit = 500) { global $wpdb; $rows = $wpdb->get_results($wpdb->prepare( "SELECT id, label FROM " . self::table() . " WHERE status <> 'archived' ORDER BY label ASC, id ASC LIMIT %d", max(1, (int) $limit) ), ARRAY_A); $out = array(); foreach (is_array($rows) ? $rows : array() as $row) { $out[(int) $row['id']] = (string) $row['label']; } return $out; } public static function flush_cache() { self::$slug_to_id_cache = null; } public static function delete($id) { global $wpdb; $id = (int) $id; if ($id <= 0) { return false; } $slug = (string) $wpdb->get_var($wpdb->prepare("SELECT slug FROM " . self::table() . " WHERE id = %d", $id)); $gone = (int) $wpdb->delete(self::table(), array('id' => $id), array('%d')); if ('' !== $slug) { wp_cache_delete('aieo_camp:' . $slug, self::CACHE_GROUP); if (self::$slug_to_id_cache !== null) { unset(self::$slug_to_id_cache[$slug]); } } if ($gone > 0) { self::unlink_ids(array($id), false); } return $gone > 0; } public static function repair_dangling_links() { global $wpdb; $camps = self::table(); if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $camps)) !== $camps) { return 0; } $dangling = array(); foreach (self::linked_tables() as $t) { $ids = $wpdb->get_col( "SELECT DISTINCT l.entry_campaign_id FROM {$t} l LEFT JOIN {$camps} c ON c.id = l.entry_campaign_id WHERE l.entry_campaign_id IS NOT NULL AND c.id IS NULL" ); foreach ((array) $ids as $cid) { $dangling[(int) $cid] = true; } } if (array() !== $dangling) { self::unlink_ids(array_keys($dangling), true); } return count($dangling); } private static function linked_tables() { global $wpdb; $out = array(); foreach (array('aieo_sessions_statics', 'aieo_dmm_offer_attributions', 'aieo_dmm_order_cancellations') as $name) { $t = $wpdb->prefix . $name; if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $t)) === $t && $wpdb->get_var("SHOW COLUMNS FROM {$t} LIKE 'entry_campaign_id'")) { $out[] = $t; } } return $out; } private static function unlink_ids(array $ids, $relink) { global $wpdb; $camps = self::table(); $sessions = $wpdb->prefix . 'aieo_sessions_statics'; foreach (self::linked_tables() as $t) { foreach ($ids as $cid) { $cid = (int) $cid; if ($relink && $t === $sessions) { $wpdb->query($wpdb->prepare( "UPDATE {$t} s JOIN {$camps} c ON c.slug = s.entry_campaign SET s.entry_campaign_id = c.id WHERE s.entry_campaign_id = %d", $cid )); } $wpdb->query($wpdb->prepare("UPDATE {$t} SET entry_campaign_id = NULL WHERE entry_campaign_id = %d", $cid)); } } } }