";
}
unset($song);
unset($artist);
unset($songalbumlinker);
die;
}
public static function conditionally_add_scripts_and_styles($posts){
GLOBAL $artistography_plugin_dir;
// scripts are already being loaded on every page
if (true == get_option('wp_artistography_ajax_loader')) return $posts;
if (empty($posts)) return $posts;
$shortcode_found = false; // use this flag to see if styles and scripts need to be enqueued
foreach ($posts as $the_post) {
if (FALSE !== stripos($the_post->post_content, '[artistography_display_song')) {
$shortcode_found = true; // bingo!
break;
}
}
if ($shortcode_found) {
// enqueue here
artistography_enqueue_style_and_scripts();
}
return $posts;
}
// [artistography_display_song id="1"]
public static function shortCodeDisplaySong( $atts ) {
extract( shortcode_atts( array(
'id' => '0'
), $atts ) );
$song = new Song;
$html = "";
$song->loadById($id);
$html .= "
";
unset($song);
return $html;
}
public function __construct() {
GLOBAL $wpdb, $TABLE_NAME;
self::$songTable = $wpdb->prefix . $TABLE_NAME[TABLE_SONGS];
$this->loadAll();
}
public function __destruct() {
/* This is a placeholder just in case it's needed eventually */
}
protected function &loadCurrNodeValues () {
GLOBAL $wpdb, $_SERVER;
if ($this->getTotalRows() > 0) {
$this->query_result = $wpdb->get_row($this->query, OBJECT, $this->getCurrNode());
// meta data info update
$this->id = $this->query_result->id;
$this->poster_id = $this->query_result->poster_id;
$this->last_updated_by_id = $this->query_result->last_updated_by_id;
$this->create_date = $this->query_result->create_date;
$this->update_date = $this->query_result->update_date;
$this->number = $this->query_result->number;
$this->artist_id = $this->query_result->artist_id;
$this->name = $this->query_result->name;
$this->url = $this->query_result->url;
$this->price = $this->query_result->price;
$this->explicit = $this->query_result->explicit;
} else {
$this->query_result =
$this->id =
$this->poster_id =
$this->last_updated_by_id =
$this->create_date =
$this->update_date =
$this->number =
$this->artist_id =
$this->name =
$this->url =
$this->price =
$this->explicit = 0;
}
return $this;
}
public function formatDateTime($day, $month, $year, $hour = 0, $min = 0, $sec = 0) {
// "YYYY-MM-DD HH:mm:SS";
if(strlen($month) < 2) $month = "0$month";
if(strlen($day) < 2) $day = "0$day";
if(strlen($hour) < 2) $hour = "0$hour";
if(strlen($min) < 2) $min = "0$min";
if(strlen($sec) < 2) $sec = "0$sec";
return "$year-$month-$day $hour:$min:$sec";
}
public function &getNodeNext () {
// when this function is used, it's assumed that a query was already run
// meant to simply load the nth item in the query through a for loop
// assumes query, query_curr_node, and query_total_rows is set
if (($this->query_curr_node + 1) < $this->getTotalRows()) {
$this->query_curr_node += 1;
$this->loadCurrNodeValues();
}
return $this;
}
public function &getNodePrev () {
// when this function is used, it's assumed that a query was already run
// meant to simply load the nth item in the query through a for loop
// assumes query, query_curr_node, and query_total_rows is set
if (($this->query_curr_node - 1) >= 0 AND $this->getTotalRows() > 0) {
$this->query_curr_node -= 1;
$this->loadCurrNodeValues();
}
return $this;
}
public function &loadByNode ($node) {
// when this function is used, it's assumed that a query was already run
// meant to simply load the nth item in the query through a for loop
if ($node < $this->getTotalRows() AND $node >= 0 AND $this->getTotalRows() > 0) {
$this->setCurrNode($node);
$this->loadCurrNodeValues();
}
return $this;
}
public function &loadById ($id, $order_by = 'id') {
GLOBAL $wpdb, $i18n_domain;
$this->query = $wpdb->prepare("SELECT *
FROM " .self::$songTable. "
WHERE id = %u
ORDER BY $order_by", $id);
$this->setTotalRows($wpdb->query($this->query));
if ($this->getTotalRows() === FALSE) wp_die( sprintf(__('An error occurred while trying to perform a query: "%s"', $i18n_domain), $this->query) );
$this->setCurrNode(0); // set to first node
return $this->loadCurrNodeValues();
}
public function &loadByPrice ($price, $order_by = 'id') {
GLOBAL $wpdb, $i18n_domain;
$this->query = $wpdb->prepare("SELECT DISTINCT *
FROM " .self::$songTable. "
WHERE price = %u
ORDER BY $order_by", $price);
$this->setTotalRows($wpdb->query($this->query));
if ($this->getTotalRows() === FALSE) wp_die( sprintf(__('An error occurred while trying to perform a query: "%s"', $i18n_domain), $this->query) );
$this->setCurrNode(0); // set to first node
return $this->loadCurrNodeValues();
}
public function &loadLastInsertId () {
GLOBAL $wpdb, $i18n_domain;
$this->query = $wpdb->prepare("SELECT LAST_INSERT_ID()");
$this->setTotalRows($wpdb->query($this->query));
if ($this->getTotalRows() === FALSE) wp_die( sprintf(__('An error occurred while trying to perform a query: "%s"', $i18n_domain), $this->query) );
$this->setCurrNode(0); // set to first node
return $this->loadCurrNodeValues();
}
public function &loadAll ($order_by = 'id') {
GLOBAL $wpdb, $i18n_domain;
$this->query = "SELECT *
FROM " .self::$songTable. "
ORDER BY $order_by";
$this->setTotalRows($wpdb->query($this->query));
if ($this->getTotalRows() === FALSE) wp_die( sprintf(__('An error occurred while trying to perform a query: "%s"', $i18n_domain), $this->query) );
$this->setCurrNode(0); // set to first node
return $this->loadCurrNodeValues();
}
public function deleteById ($id) {
GLOBAL $wpdb, $i18n_domain;
$this->query = $wpdb->prepare(
"DELETE FROM " .self::$songTable. "
WHERE id = %u", $id);
$this->setTotalRows($wpdb->query($this->query));
if ($this->getTotalRows() === FALSE) {
wp_die( sprintf(__('An error occurred while trying to perform a query: "%s"', $i18n_domain), $this->query) );
}
return $this->getTotalRows();
}
// update artist entry
public function updateById ($song_id, $number, $artist_id, $song_name, $url, $price, $explicit) {
GLOBAL $wpdb, $i18n_domain;
GLOBAL $current_user;
get_currentuserinfo();
$this->query = $wpdb->prepare(
"UPDATE " .self::$songTable. "
SET update_date = now(),
number = %u,
artist_id = %u,
name = %s,
url = %s,
price = %s,
explicit = %b,
last_updated_by_id = %u
WHERE id = %s",
(int)$number, $artist_id, $song_name, $url, $price, $explicit, $current_user->ID, $song_id);
$this->setTotalRows($wpdb->query($this->query));
if ($this->getTotalRows() === FALSE) {
wp_die( sprintf(__('An error occurred while trying to perform a query: "%s"', $i18n_domain), $this->query) );
} else {
// was entered successfully into database
return true;
}
}
public function insert ($number, $artist_id, $song_name, $url, $price, $explicit) {
GLOBAL $wpdb, $i18n_domain;
GLOBAL $current_user;
get_currentuserinfo();
$this->query = $wpdb->prepare(
"INSERT INTO " .self::$songTable. "
(create_date, update_date, poster_id, last_updated_by_id, number, artist_id, name, url, price, explicit)
VALUES (now(), now(), %u, %u, %u, %s, %s, %s, %s, %b)",
$current_user->ID, $current_user->ID, (int)$number, $artist_id, $song_name, $url, $price, $explicit);
$this->setTotalRows($wpdb->query($this->query));
if ($this->getTotalRows() === 0 OR $this->getTotalRows() === FALSE) {
return sprintf(__('An error occurred while trying to perform a query: "%s"', $i18n_domain), $this->query);
} else {
// was entered successfully into database
$this->loadLastInsertId();
return $this->id;
}
}
} /* end class Song */
?>