2009-08-09 16:39:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WhatDidTheySay manages transcriptions that are attached as metadata
|
|
|
|
* individual posts. Transcriptions can be updated, deleted, and moved to
|
|
|
|
* other posts as necessary.
|
|
|
|
*/
|
|
|
|
class WhatDidTheySay {
|
2009-08-12 23:51:23 +00:00
|
|
|
var $version = "0.1";
|
2009-08-19 23:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2009-08-09 23:53:44 +00:00
|
|
|
function WhatDidTheySay() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$this->table = $wpdb->prefix . "provided_transcripts";
|
|
|
|
}
|
2009-08-19 23:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle plugin activation.
|
|
|
|
*/
|
2009-08-12 23:51:23 +00:00
|
|
|
function install() {
|
|
|
|
if (get_option('what-did-they-say-version') !== $this->version) {
|
|
|
|
$sql = "CREATE TABLE %s (
|
|
|
|
id int NOT NULL AUTO_INCREMENT,
|
|
|
|
post_id int NOT NULL,
|
|
|
|
user_id int NOT NULL,
|
|
|
|
language char(10) NOT NULL,
|
|
|
|
transcript mediumtext,
|
|
|
|
UNIQUE KEY id (id)
|
|
|
|
);";
|
|
|
|
|
|
|
|
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
2009-08-13 16:37:33 +00:00
|
|
|
dbDelta(sprintf($sql, $this->table));
|
2009-08-12 23:51:23 +00:00
|
|
|
|
2009-08-13 16:37:33 +00:00
|
|
|
update_option('what-did-they-say-version', $this->version);
|
2009-08-12 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-11 13:28:16 +00:00
|
|
|
/**
|
|
|
|
* Save a transcript to a post.
|
|
|
|
* @param int $post_id The post to attach the transcript to.
|
|
|
|
* @param string $language The language of the transcript.
|
|
|
|
* @param string $transcript The transcript content.
|
|
|
|
* @return bool True if the transcript was saved, false otherwise.
|
|
|
|
*/
|
2009-08-09 16:39:52 +00:00
|
|
|
function save_transcript($post_id, $language, $transcript) {
|
2009-08-19 00:21:21 +00:00
|
|
|
if (current_user_can('submit_transcriptions')) {
|
2009-08-11 22:44:47 +00:00
|
|
|
$post = get_post($post_id);
|
|
|
|
if (!empty($post)) {
|
|
|
|
$current_transcripts = get_post_meta($post_id, "provided_transcripts", true);
|
|
|
|
if (!is_array($current_transcripts)) { $current_transcripts = array(); }
|
|
|
|
$current_transcripts[$language] = $transcript;
|
|
|
|
return update_post_meta($post_id, "provided_transcripts", $current_transcripts);
|
|
|
|
}
|
|
|
|
return false;
|
2009-08-11 13:28:16 +00:00
|
|
|
}
|
2009-08-09 16:39:52 +00:00
|
|
|
}
|
2009-08-15 20:45:08 +00:00
|
|
|
|
2009-08-19 23:58:24 +00:00
|
|
|
/**
|
|
|
|
* Get all transcripts for the indicated post.
|
|
|
|
* @param int $post_id The post ID to search.
|
|
|
|
* @return array|false The provided transcripts for the post, or false if no transcripts found.
|
|
|
|
*/
|
2009-08-15 20:45:08 +00:00
|
|
|
function get_transcripts($post_id) {
|
|
|
|
return get_post_meta($post_id, 'provided_transcripts', true);
|
|
|
|
}
|
2009-08-09 23:53:44 +00:00
|
|
|
|
2009-08-11 13:28:16 +00:00
|
|
|
/**
|
|
|
|
* Get the languages that the approved transcripts for the post are written in.
|
|
|
|
* @param int $post_id The post ID to check for transcripts.
|
|
|
|
* @return array|false The languages for the transcripts, or false if none found.
|
|
|
|
*/
|
2009-08-09 23:53:44 +00:00
|
|
|
function get_transcript_languages($post_id) {
|
|
|
|
$current_transcripts = get_post_meta($post_id, "provided_transcripts", true);
|
|
|
|
if (is_array($current_transcripts)) {
|
|
|
|
return array_keys($current_transcripts);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-11 13:28:16 +00:00
|
|
|
/**
|
|
|
|
* Get the queued transcriptions for the provided post.
|
|
|
|
* @param int $post_id The post to search for transcripts.
|
|
|
|
* @return array|false The array of transcripts for the post, or false if the post is invalid.
|
|
|
|
*/
|
2009-08-09 23:53:44 +00:00
|
|
|
function get_queued_transcriptions_for_post($post_id) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2009-08-19 00:21:21 +00:00
|
|
|
if (current_user_can('submit_transcriptions')) {
|
2009-08-11 22:44:47 +00:00
|
|
|
$post = get_post($post_id);
|
|
|
|
if (!empty($post)) {
|
2009-08-19 02:19:11 +00:00
|
|
|
$query = $wpdb->prepare('SELECT * FROM ' . $this->table . ' WHERE post_id = %d', $post_id);
|
2009-08-11 22:44:47 +00:00
|
|
|
$results = $wpdb->get_results($query);
|
|
|
|
if (!empty($results)) {
|
|
|
|
$valid_results = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$user = get_userdata($result->user_id);
|
|
|
|
if (!empty($user)) {
|
|
|
|
$valid_results[] = $result;
|
|
|
|
}
|
2009-08-09 23:53:44 +00:00
|
|
|
}
|
2009-08-11 22:44:47 +00:00
|
|
|
return $valid_results;
|
2009-08-09 23:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-10 16:50:30 +00:00
|
|
|
|
2009-08-11 13:28:16 +00:00
|
|
|
/**
|
|
|
|
* Queue a transcription to a post.
|
|
|
|
* @param int $post_id The post to attach the transcription to.
|
|
|
|
* @param array $transcript_info The new transcript's info.
|
|
|
|
*/
|
2009-08-10 16:50:30 +00:00
|
|
|
function add_queued_transcription_to_post($post_id, $transcript_info) {
|
|
|
|
global $wpdb;
|
2009-08-19 02:19:11 +00:00
|
|
|
|
2009-08-19 00:21:21 +00:00
|
|
|
if (current_user_can('approve_transcriptions')) {
|
2009-08-11 22:44:47 +00:00
|
|
|
$post = get_post($post_id);
|
|
|
|
if (!empty($post)) {
|
|
|
|
$transcript_info = (array)$transcript_info;
|
|
|
|
if (!empty($transcript_info)) {
|
|
|
|
$ok = true;
|
|
|
|
foreach (array('language', 'transcript') as $field) {
|
|
|
|
if (empty($transcript_info[$field])) { $ok = false; break; }
|
|
|
|
}
|
|
|
|
if ($ok) {
|
|
|
|
extract($transcript_info);
|
2009-08-19 02:19:11 +00:00
|
|
|
$user = wp_get_current_user();
|
2009-08-11 22:44:47 +00:00
|
|
|
if (!empty($user)) {
|
|
|
|
$query = $wpdb->prepare(
|
2009-08-19 02:19:11 +00:00
|
|
|
"INSERT INTO " . $this->table . "(post_id, user_id, language, transcript) VALUES (%d, %d, %s, %s)",
|
|
|
|
$post_id, $user->ID, $language, $transcript
|
2009-08-11 22:44:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $wpdb->query($query);
|
|
|
|
}
|
2009-08-10 16:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-11 13:14:37 +00:00
|
|
|
|
2009-08-11 21:14:46 +00:00
|
|
|
/**
|
|
|
|
* Update a queued transcript.
|
|
|
|
* @param array $update_info The info on the transcript being updated.
|
2009-08-11 22:18:33 +00:00
|
|
|
* @return boolean True if the transcript was updated.
|
2009-08-11 21:14:46 +00:00
|
|
|
*/
|
2009-08-11 22:16:23 +00:00
|
|
|
function update_queued_transcription($update_info) {
|
2009-08-11 21:14:46 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
2009-08-19 00:21:21 +00:00
|
|
|
if (current_user_can('submit_transcriptions')) {
|
2009-08-19 02:19:11 +00:00
|
|
|
$query = $wpdb->prepare("SELECT * FROM " . $this->table . " WHERE id = %d", $update_info['id']);
|
2009-08-11 13:14:37 +00:00
|
|
|
$result = $wpdb->get_results($query);
|
|
|
|
|
2009-08-12 23:02:36 +00:00
|
|
|
if (is_array($result)) {
|
2009-08-11 13:14:37 +00:00
|
|
|
if (count($result) == 1) {
|
|
|
|
$result = $result[0];
|
|
|
|
foreach (array('language', 'transcript') as $field) {
|
|
|
|
$result->{$field} = $update_info[$field];
|
|
|
|
}
|
|
|
|
$query = $wpdb->prepare(
|
2009-08-19 02:19:11 +00:00
|
|
|
"UPDATE " . $this->table . " SET language = %s, transcript = %s WHERE id = %d",
|
|
|
|
$result->language, $result->transcript, $result->id
|
2009-08-11 13:14:37 +00:00
|
|
|
);
|
|
|
|
$wpdb->query($query);
|
2009-08-11 22:18:33 +00:00
|
|
|
return true;
|
2009-08-11 13:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-11 22:18:33 +00:00
|
|
|
return false;
|
2009-08-11 13:14:37 +00:00
|
|
|
}
|
2009-08-11 22:16:23 +00:00
|
|
|
|
2009-08-11 22:18:33 +00:00
|
|
|
/**
|
|
|
|
* Delete a queued transcript.
|
|
|
|
* @param int $transcription_id The transcription to delete.
|
|
|
|
* @return boolean True if the transcript was deleted.
|
|
|
|
*/
|
2009-08-11 22:16:23 +00:00
|
|
|
function delete_queued_transcription($transcription_id) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2009-08-19 00:21:21 +00:00
|
|
|
if (current_user_can('submit_transcriptions')) {
|
2009-08-19 22:54:12 +00:00
|
|
|
$query = $wpdb->prepare("DELETE FROM " . $this->table . " WHERE id = %d", $transcription_id);
|
|
|
|
return $wpdb->query($query);
|
2009-08-11 22:16:23 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-19 23:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a queued transcription to its associated post.
|
|
|
|
* @param int $transcription_id The transcription ID to approve.
|
|
|
|
*/
|
2009-08-12 23:02:36 +00:00
|
|
|
function add_transcription_to_post($transcription_id) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2009-08-19 00:21:21 +00:00
|
|
|
if (current_user_can('approve_transcriptions')) {
|
2009-08-19 02:19:11 +00:00
|
|
|
$query = $wpdb->prepare("SELECT * from " . $this->table . " WHERE id = %d", $transcription_id);
|
2009-08-12 23:02:36 +00:00
|
|
|
$result = $wpdb->get_results($query);
|
|
|
|
if (is_array($result)) {
|
|
|
|
if (count($result) == 1) {
|
|
|
|
$result = (object)$result[0];
|
|
|
|
|
|
|
|
$post = get_post($result->post_id);
|
|
|
|
if (!empty($post)) {
|
|
|
|
$this->save_transcript($result->post_id, $result->language, $result->transcript);
|
2009-08-19 22:54:12 +00:00
|
|
|
$this->delete_queued_transcription($transcription_id);
|
|
|
|
}
|
2009-08-12 23:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-19 02:19:11 +00:00
|
|
|
|
2009-08-19 23:58:24 +00:00
|
|
|
/**
|
|
|
|
* Get all the queued transcriptions for a particular user and post.
|
|
|
|
* @param int $user_id The user ID to search for.
|
|
|
|
* @param int $post_id The post ID to search for.
|
|
|
|
* @return array|false The queued transcriptions for this user, or false if they don't have permissions to see queued transcriptions.
|
|
|
|
*/
|
2009-08-19 02:19:11 +00:00
|
|
|
function get_queued_transcriptions_for_user_and_post($user_id, $post_id) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if (current_user_can('submit_transcriptions')) {
|
|
|
|
$query = $wpdb->prepare("SELECT * FROM " . $this->table . " WHERE user_id = %d AND post_id = %d", $user_id, $post_id);
|
|
|
|
return $wpdb->get_results($query);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-19 23:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a transcript for a particular language from a post.
|
|
|
|
* @param int $post_id The post to search.
|
|
|
|
* @param string $language The language code to search for.
|
|
|
|
* @return boolean True if the transcript was deleted.
|
|
|
|
*/
|
2009-08-12 23:12:52 +00:00
|
|
|
function delete_transcript($post_id, $language) {
|
2009-08-19 00:21:21 +00:00
|
|
|
if (current_user_can('approve_transcriptions')) {
|
2009-08-12 23:12:52 +00:00
|
|
|
$post = get_post($post_id);
|
|
|
|
if (!empty($post)) {
|
|
|
|
$current_transcripts = get_post_meta($post_id, "provided_transcripts", true);
|
|
|
|
unset($current_transcripts[$language]);
|
|
|
|
update_post_meta($post_id, "provided_transcripts", $current_transcripts);
|
2009-08-19 23:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-08-12 23:12:52 +00:00
|
|
|
}
|
2009-08-19 23:58:24 +00:00
|
|
|
return false;
|
2009-08-12 23:12:52 +00:00
|
|
|
}
|
2009-08-19 23:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default transcript language for this blog.
|
|
|
|
* @return string The language code representing the default language.
|
|
|
|
*/
|
2009-08-16 18:14:07 +00:00
|
|
|
function get_default_language() {
|
|
|
|
$language = false;
|
|
|
|
$options = get_option('what-did-they-say-options');
|
|
|
|
foreach ($options['languages'] as $code => $info) {
|
|
|
|
if (is_null($language)) { $language = $code; }
|
|
|
|
if ($info['default']) { $language = $code; break; }
|
|
|
|
}
|
|
|
|
return $language;
|
|
|
|
}
|
2009-08-19 23:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of a language from the language code.
|
|
|
|
* @param string $language The language code to search for.
|
|
|
|
* @return string|false The name of the language as defined in the options, or false if the language was not found.
|
|
|
|
*/
|
2009-08-16 18:14:07 +00:00
|
|
|
function get_language_name($language) {
|
|
|
|
$options = get_option('what-did-they-say-options');
|
|
|
|
|
|
|
|
if (isset($options['languages'][$language])) {
|
|
|
|
return $options['languages'][$language]['name'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-08-19 23:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all available languages.
|
|
|
|
* @return array An array of languages.
|
|
|
|
*/
|
2009-08-16 20:54:11 +00:00
|
|
|
function get_languages() {
|
|
|
|
$options = get_option('what-did-they-say-options');
|
|
|
|
|
|
|
|
return $options['languages'];
|
|
|
|
}
|
2009-08-19 02:33:59 +00:00
|
|
|
|
2009-08-19 23:58:24 +00:00
|
|
|
/**
|
|
|
|
* Set whether or not the indicated post can accept new queued transcriptions.
|
|
|
|
* @param int $post_id The post ID to affect.
|
|
|
|
* @param boolean $allow True if the post can accept new queued transcriptions.
|
|
|
|
*/
|
2009-08-19 02:33:59 +00:00
|
|
|
function set_allow_transcripts_for_post($post_id, $allow = true) {
|
|
|
|
$current_transcripts = get_post_meta($post_id, "provided_transcripts", true);
|
|
|
|
$current_transcripts['_allow'] = $allow;
|
|
|
|
update_post_meta($post_id, "provided_transcripts", $current_transcripts);
|
|
|
|
}
|
|
|
|
|
2009-08-19 23:58:24 +00:00
|
|
|
/**
|
|
|
|
* See if the indicated post is accepting new transcripts.
|
|
|
|
* @return boolean True if the post is acceptin new transcripts.
|
|
|
|
*/
|
2009-08-19 02:33:59 +00:00
|
|
|
function get_allow_transcripts_for_post($post_id) {
|
|
|
|
$current_transcripts = get_post_meta($post_id, "provided_transcripts", true);
|
|
|
|
return $current_transcripts['_allow'];
|
|
|
|
}
|
2009-08-09 16:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|