예제 #1
0
function wppa_add($key, $newval)
{
    global $wppa;
    // Array defined?
    if (empty($wppa)) {
        wppa_reset_occurrance();
    }
    // Valid key?
    if (isset($wppa[$key])) {
        // Get old value
        $oldval = $wppa[$key];
        // Add new value
        $wppa[$key] .= $newval;
    } else {
        wppa_log('Err', '$wppa[\'' . $key . '\'] is not defined', true);
        return false;
    }
    return $oldval;
}
 /** @see WP_Widget::widget */
 function widget($args, $instance)
 {
     global $wpdb;
     require_once dirname(__FILE__) . '/wppa-links.php';
     require_once dirname(__FILE__) . '/wppa-styles.php';
     require_once dirname(__FILE__) . '/wppa-functions.php';
     require_once dirname(__FILE__) . '/wppa-thumbnails.php';
     require_once dirname(__FILE__) . '/wppa-boxes-html.php';
     require_once dirname(__FILE__) . '/wppa-slideshow.php';
     wppa_initialize_runtime();
     wppa('in_widget', 'topten');
     wppa_bump_mocc();
     extract($args);
     $instance = wp_parse_args((array) $instance, array('title' => '', 'sortby' => 'mean_rating', 'title' => '', 'album' => '', 'display' => 'thumbs', 'meanrat' => 'yes', 'ratcount' => 'yes', 'viewcount' => 'yes', 'includesubs' => 'yes', 'medalsonly' => 'no', 'showowner' => 'no', 'showalbum' => 'no'));
     $widget_title = apply_filters('widget_title', $instance['title']);
     $page = in_array(wppa_opt('topten_widget_linktype'), wppa('links_no_page')) ? '' : wppa_get_the_landing_page('topten_widget_linkpage', __('Top Ten Photos', 'wp-photo-album-plus'));
     $albumlinkpage = wppa_get_the_landing_page('topten_widget_album_linkpage', __('Top Ten Photo album', 'wp-photo-album-plus'));
     $max = wppa_opt('topten_count');
     $album = $instance['album'];
     switch ($instance['sortby']) {
         case 'mean_rating':
             $sortby = '`mean_rating` DESC, `rating_count` DESC, `views` DESC';
             break;
         case 'rating_count':
             $sortby = '`rating_count` DESC, `mean_rating` DESC, `views` DESC';
             break;
         case 'views':
             $sortby = '`views` DESC, `mean_rating` DESC, `rating_count` DESC';
             break;
     }
     $display = $instance['display'];
     $meanrat = $instance['meanrat'] == 'yes';
     $ratcount = $instance['ratcount'] == 'yes';
     $viewcount = $instance['viewcount'] == 'yes';
     $includesubs = $instance['includesubs'] == 'yes';
     $albenum = '';
     $medalsonly = $instance['medalsonly'] == 'yes';
     $showowner = $instance['showowner'] == 'yes';
     $showalbum = $instance['showalbum'] == 'yes';
     wppa('medals_only', $medalsonly);
     $likes = wppa_opt('rating_display_type') == 'likes';
     // When likes only, mean rating has no meaning, chan to (rating)(like)count
     if ($likes && $instance['sortby'] == 'mean_rating') {
         $instance['sortby'] = 'rating_count';
     }
     // Album specified?
     if ($album) {
         // All albums ?
         if ($album == '-2') {
             $album = '0';
         }
         // Albums of owner is current logged in user or public?
         if ($album == '-3') {
             $temp = $wpdb->get_results("SELECT `id` FROM `" . WPPA_ALBUMS . "` WHERE `owner` = '--- public ---' OR `owner` = '" . wppa_get_user() . "' ORDER BY `id`", ARRAY_A);
             $album = '';
             if ($temp) {
                 foreach ($temp as $t) {
                     $album .= '.' . $t['id'];
                 }
                 $album = ltrim($album, '.');
             }
         }
         // Including subalbums?
         if ($includesubs) {
             $albenum = wppa_alb_to_enum_children($album);
             $albenum = wppa_expand_enum($albenum);
             $album = str_replace('.', ',', $albenum);
         }
         // Doit
         if ($medalsonly) {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN (" . $album . ") " . "AND `status` IN ( 'gold', 'silver', 'bronze' ) " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         } else {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN (" . $album . ") " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         }
     } else {
         if ($medalsonly) {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `status` IN ( 'gold', 'silver', 'bronze' ) " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         } else {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         }
     }
     $widget_content = "\n" . '<!-- WPPA+ TopTen Widget start -->';
     $maxw = wppa_opt('topten_size');
     $maxh = $maxw;
     $lineheight = wppa_opt('fontsize_widget_thumb') * 1.5;
     $maxh += $lineheight;
     if ($meanrat) {
         $maxh += $lineheight;
     }
     if ($ratcount) {
         $maxh += $lineheight;
     }
     if ($viewcount) {
         $maxh += $lineheight;
     }
     if ($showowner) {
         $maxh += $lineheight;
     }
     if ($showalbum) {
         $maxh += $lineheight;
     }
     if ($thumbs) {
         foreach ($thumbs as $image) {
             $thumb = $image;
             // Make the HTML for current picture
             if ($display == 'thumbs') {
                 $widget_content .= "\n" . '<div class="wppa-widget" style="width:' . $maxw . 'px; height:' . $maxh . 'px; margin:4px; display:inline; text-align:center; float:left;">';
             } else {
                 $widget_content .= "\n" . '<div class="wppa-widget" >';
             }
             if ($image) {
                 $no_album = !$album;
                 if ($no_album) {
                     $tit = __('View the top rated photos', 'wp-photo-album-plus');
                 } else {
                     $tit = esc_attr(__(stripslashes($image['description'])));
                 }
                 $compressed_albumenum = wppa_compress_enum($albenum);
                 $link = wppa_get_imglnk_a('topten', $image['id'], '', $tit, '', $no_album, $compressed_albumenum);
                 $file = wppa_get_thumb_path($image['id']);
                 $imgstyle_a = wppa_get_imgstyle_a($image['id'], $file, $maxw, 'center', 'ttthumb');
                 $imgurl = wppa_get_thumb_url($image['id'], '', $imgstyle_a['width'], $imgstyle_a['height']);
                 $imgevents = wppa_get_imgevents('thumb', $image['id'], true);
                 $title = $link ? esc_attr(stripslashes($link['title'])) : '';
                 $widget_content .= wppa_get_the_widget_thumb('topten', $image, $album, $display, $link, $title, $imgurl, $imgstyle_a, $imgevents);
                 $widget_content .= "\n\t" . '<div style="font-size:' . wppa_opt('fontsize_widget_thumb') . 'px; line-height:' . $lineheight . 'px;">';
                 // Display (owner) ?
                 if ($showowner) {
                     $widget_content .= '<div>(' . $image['owner'] . ')</div>';
                 }
                 // Display (album) ?
                 if ($showalbum) {
                     $href = wppa_convert_to_pretty(wppa_encrypt_url(wppa_get_album_url($image['album'], $albumlinkpage, 'content', '1')));
                     $widget_content .= '<div>(<a href="' . $href . '" >' . wppa_get_album_name($image['album']) . '</a>)</div>';
                 }
                 // Display the rating
                 if ($likes) {
                     $lt = wppa_get_like_title_a($image['id']);
                 }
                 switch ($instance['sortby']) {
                     case 'mean_rating':
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         break;
                     case 'rating_count':
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . ($likes ? $lt['display'] : sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n)) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         break;
                     case 'views':
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . ($likes ? $lt['display'] : sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n)) . '</div>';
                         }
                         break;
                 }
                 $widget_content .= '</div>';
             } else {
                 // No image
                 $widget_content .= __('Photo not found', 'wp-photo-album-plus');
             }
             $widget_content .= "\n" . '</div>';
         }
     } else {
         $widget_content .= __('There are no rated photos (yet)', 'wp-photo-album-plus');
     }
     $widget_content .= '<div style="clear:both"></div>';
     $widget_content .= "\n" . '<!-- WPPA+ TopTen Widget end -->';
     echo "\n" . $before_widget;
     if (!empty($widget_title)) {
         echo $before_title . $widget_title . $after_title;
     }
     echo $widget_content . $after_widget;
     //wppa( 'in_widget', false );
     wppa_reset_occurrance();
 }
function wppa_album_name_to_number($xalb)
{
    $xalb = strip_tags($xalb);
    if ($xalb && !wppa_is_int($xalb)) {
        if (substr($xalb, 0, 1) == '$') {
            // Name
            $id = wppa_get_album_id_by_name(substr($xalb, 1), 'report_dups');
            if ($id > '0') {
                return $id;
            } elseif ($id < '0') {
                wppa_dbg_msg('Duplicate album names found: ' . $xalb, 'red', 'force');
                wppa_reset_occurrance();
                return false;
                // Forget this occurrance
            } else {
                wppa_dbg_msg('Album name not found: ' . $xalb, 'red', 'force');
                wppa_reset_occurrance();
                return false;
                // Forget this occurrance
            }
        } else {
            return $xalb;
        }
        // Is album enum
    } else {
        return $xalb;
    }
    // Is non zero integer
}
function wppa_album_name_to_number($xalb, $return_dups = false)
{
    // Sanitize
    $xalb = strip_tags($xalb);
    // Any non integer input left?
    if ($xalb && !wppa_is_int($xalb)) {
        // Is it a name?
        if (substr($xalb, 0, 1) == '$') {
            if ($return_dups) {
                $id = wppa_get_album_id_by_name(substr($xalb, 1), 'return_dups');
            } else {
                $id = wppa_get_album_id_by_name(substr($xalb, 1));
            }
            // Anything found?
            if ($id > '0') {
                return $id;
            } elseif ($id < '0') {
                wppa_dbg_msg('Duplicate album names found: ' . $xalb, 'red', 'force');
                wppa_reset_occurrance();
                return false;
                // Forget this occurrance
            } else {
                wppa_dbg_msg('Album name not found: ' . $xalb, 'red', 'force');
                wppa_reset_occurrance();
                return false;
                // Forget this occurrance
            }
        } else {
            return $xalb;
        }
        // Is album enum
    } else {
        return $xalb;
    }
    // Is non zero integer
}
function wppa_set_shortcodes($xatts, $content = '')
{
    global $wppa;
    global $wppa_opt;
    $atts = shortcode_atts(array('name' => '', 'value' => ''), $xatts);
    $allowed = explode(',', wppa_opt('set_shortcodes'));
    // Valid item?
    if ($atts['name'] && !in_array($atts['name'], $allowed)) {
        wppa_dbg_msg($atts['name'] . ' is not a runtime settable configuration entity.', 'red', 'force');
    } elseif (!$atts['name']) {
        $wppa_opt = get_option('wppa_cached_options', false);
        wppa_reset_occurrance();
    } elseif (substr($atts['name'], 0, 5) == 'wppa_') {
        if (isset($wppa_opt[$atts['name']])) {
            $wppa_opt[$atts['name']] = $atts['value'];
        } else {
            wppa_dbg_msg($atts['name'] . ' is not an option value.', 'red', 'force');
        }
    } else {
        if (isset($wppa[$atts['name']])) {
            $wppa[$atts['name']] = $value;
        } else {
            wppa_dbg_msg($atts['name'] . ' is not a runtime value.', 'red', 'force');
        }
    }
}
function wppa_initialize_runtime($force = false)
{
    global $wppa;
    global $wppa_opt;
    global $wppa_revno;
    global $wppa_api_version;
    global $wpdb;
    global $wppa_initruntimetime;
    global $wppa_defaults;
    $wppa_initruntimetime = -microtime(true);
    if ($force) {
        $wppa = false;
        // destroy existing arrays
        $wppa_opt = false;
        delete_option('wppa_cached_options');
    }
    if (is_array($wppa) && !$force) {
        return;
        // Done already
    }
    if (!is_array($wppa)) {
        wppa_reset_occurrance();
    }
    // Get the cache version of all settings
    $wppa_opt = get_option('wppa_cached_options', false);
    // Check for validity, only on admin pages (due to qTranslate behaviour), non ajax (to keep performance at front-end ajax).
    if (is_admin() && !defined('DOING_AJAX')) {
        if (is_array($wppa_opt) && md5(serialize($wppa_opt)) != get_option('wppa_md5_options', 'nil')) {
            // Log hash error
            wppa_log('Obs', 'Read hash:' . get_option('wppa_md5_options', 'nil') . ', computed hash:' . md5(serialize($wppa_opt)));
            // Something wrong. Let us see what, if not intentional!
            if (!$force) {
                foreach (array_keys($wppa_opt) as $key) {
                    if ($wppa_opt[$key] != get_option($key)) {
                        wppa_log('dbg', 'Corrupted setting found. Cached value=' . $wppa_opt[$key] . ', option value=' . get_option($key));
                    }
                }
            }
            $count = count($wppa_opt);
            // Report fix only if not intentional, with stacktrace
            if (!$force) {
                wppa_log('Fix', 'Option cache. Count=' . $count);
            }
            // Clear cached options to force rebuild
            $wppa_opt = false;
        }
    }
    // Rebuild cached options if required, i.e. when not yet existing or deleted.
    if (!is_array($wppa_opt)) {
        wppa_set_defaults();
        $wppa_opt = $wppa_defaults;
        foreach (array_keys($wppa_opt) as $option) {
            $optval = get_option($option, 'nil');
            if ($optval !== 'nil') {
                $wppa_opt[$option] = $optval;
            }
        }
        update_option('wppa_cached_options', $wppa_opt, true);
        update_option('wppa_md5_options', md5(serialize($wppa_opt)), true);
        // Verify success
        $temp = get_option('wppa_cached_options');
        $hash = get_option('wppa_md5_options');
        if (md5(serialize($temp)) != $hash) {
            wppa_log('Err', 'Discrepancy found. Count=' . count($temp));
        }
    }
    if (isset($_GET['debug']) && wppa_switch('allow_debug')) {
        $key = $_GET['debug'] ? $_GET['debug'] : E_ALL;
        wppa('debug', $key);
    }
    // Delete obsolete spam
    $spammaxage = wppa_opt('spam_maxage');
    if ($spammaxage != 'none') {
        $time = time();
        $obsolete = $time - $spammaxage;
        $iret = $wpdb->query($wpdb->prepare("DELETE FROM `" . WPPA_COMMENTS . "` WHERE `status` = 'spam' AND `timestamp` < %s", $obsolete));
        if ($iret) {
            wppa_update_option('wppa_spam_auto_delcount', get_option('wppa_spam_auto_delcount', '0') + $iret);
        }
    }
    $wppa_initruntimetime += microtime(true);
}
예제 #7
0
function wppa_initialize_runtime($force = false)
{
    global $wppa;
    global $wppa_opt;
    global $wppa_revno;
    global $wppa_api_version;
    global $wpdb;
    global $wppa_initruntimetime;
    global $wppa_defaults;
    $wppa_initruntimetime = -microtime(true);
    if ($force) {
        $wppa = false;
        // destroy existing arrays
        $wppa_opt = false;
        delete_option('wppa_cached_options');
    }
    if (is_array($wppa) && !$force) {
        return;
        // Done already
    }
    if (!is_array($wppa)) {
        wppa_reset_occurrance();
    }
    $wppa_opt = get_option('wppa_cached_options', false);
    if (!is_array($wppa_opt)) {
        wppa_set_defaults();
        $wppa_opt = $wppa_defaults;
        foreach (array_keys($wppa_opt) as $option) {
            $optval = get_option($option, 'nil');
            if ($optval !== 'nil') {
                $wppa_opt[$option] = $optval;
            }
        }
        update_option('wppa_cached_options', $wppa_opt);
    }
    if (isset($_GET['debug']) && wppa_switch('allow_debug')) {
        $key = $_GET['debug'] ? $_GET['debug'] : E_ALL;
        wppa('debug', $key);
    }
    // Delete obsolete spam
    $spammaxage = wppa_opt('spam_maxage');
    if ($spammaxage != 'none') {
        $time = time();
        $obsolete = $time - $spammaxage;
        $iret = $wpdb->query($wpdb->prepare("DELETE FROM `" . WPPA_COMMENTS . "` WHERE `status` = 'spam' AND `timestamp` < %s", $obsolete));
        if ($iret) {
            wppa_update_option('wppa_spam_auto_delcount', get_option('wppa_spam_auto_delcount', '0') + $iret);
        }
    }
    // Create an album if required
    if (wppa_switch('grant_an_album') && wppa_switch('owner_only') && is_user_logged_in() && (current_user_can('wppa_upload') || wppa_switch('user_upload_on'))) {
        $owner = wppa_get_user('login');
        $user = wppa_get_user(wppa_opt('grant_name'));
        $albs = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_ALBUMS . "` WHERE `owner` = %s", $owner));
        if (!$albs) {
            // make an album for this user
            $name = $user;
            if (is_admin()) {
                $desc = __('Default photo album for', 'wp-photo-album-plus') . ' ' . $user;
            } else {
                $desc = __('Default photo album for', 'wp-photo-album-plus') . ' ' . $user;
            }
            $parent = wppa_opt('grant_parent');
            $id = wppa_create_album_entry(array('name' => $name, 'description' => $desc, 'a_parent' => $parent));
            wppa_flush_treecounts($parent);
            wppa_index_add('album', $id);
        }
    }
    $wppa_initruntimetime += microtime(true);
}