コード例 #1
0
        /**
         * Generates the main JavaScript which renders all the ratings on the page.
         *
         * @param bool $pElement
         */
        function rw_attach_rating_js($pElement = false)
        {
            if (RWLogger::IsOn()) {
                $params = func_get_args();
                RWLogger::LogEnterence("rw_attach_rating_js", $params);
            }
            $rw_settings = array("blog-post" => array("options" => WP_RW__BLOG_POSTS_OPTIONS), "front-post" => array("options" => WP_RW__FRONT_POSTS_OPTIONS), "comment" => array("options" => WP_RW__COMMENTS_OPTIONS), "page" => array("options" => WP_RW__PAGES_OPTIONS), "activity-update" => array("options" => WP_RW__ACTIVITY_UPDATES_OPTIONS), "activity-comment" => array("options" => WP_RW__ACTIVITY_COMMENTS_OPTIONS), "new-forum-post" => array("options" => WP_RW__ACTIVITY_FORUM_POSTS_OPTIONS), "new-blog-post" => array("options" => WP_RW__ACTIVITY_BLOG_POSTS_OPTIONS), "new-blog-comment" => array("options" => WP_RW__ACTIVITY_BLOG_COMMENTS_OPTIONS), "forum-post" => array("options" => WP_RW__ACTIVITY_FORUM_POSTS_OPTIONS), "forum-reply" => array("options" => WP_RW__ACTIVITY_FORUM_POSTS_OPTIONS), "user" => array("options" => WP_RW__USERS_OPTIONS), "user-post" => array("options" => WP_RW__USERS_POSTS_OPTIONS), "user-page" => array("options" => WP_RW__USERS_PAGES_OPTIONS), "user-comment" => array("options" => WP_RW__USERS_COMMENTS_OPTIONS), "user-activity-update" => array("options" => WP_RW__USERS_ACTIVITY_UPDATES_OPTIONS), "user-activity-comment" => array("options" => WP_RW__USERS_ACTIVITY_COMMENTS_OPTIONS), "user-forum-post" => array("options" => WP_RW__USERS_FORUM_POSTS_OPTIONS));
            foreach ($this->_extensions as $ext) {
                $ext_settings = $ext->GetSettings();
                foreach ($ext_settings as $type => $options) {
                    $rw_settings[$options['class']] = array('options' => $options['options']);
                }
            }
            $attach_js = false;
            $criteria_suffix_part = '-criteria';
            if (is_array(self::$ratings) && count(self::$ratings) > 0) {
                foreach (self::$ratings as $urid => $data) {
                    $rclass = $data["rclass"];
                    if (RWLogger::IsOn()) {
                        RWLogger::Log('rw_attach_rating_js', 'Urid = ' . $urid . '; Class = ' . $rclass . ';');
                    }
                    $suffix_pos = strpos($rclass, $criteria_suffix_part);
                    if (false !== $suffix_pos) {
                        /* Use dummy value for the criteria options but
                         * use the settings of the summary rating when
                         * calling RW.initClass below
                         */
                        $rw_settings[$rclass] = 'DUMMY';
                        /*
                         * Make sure that the following code (the if block) will have the main option class, e.g. blog-post,
                         * and not the criterion class, e.g. blog-post-criteria-1. This is because the following
                         * code needs the main option class in order to load the type (blog, page, etc.) settings which include
                         * the themes and other display options. A criterion rating will use these display options,
                         * so this code extracts the main option class from the criterion class.
                         */
                        $rclass = substr($rclass, 0, $suffix_pos);
                    }
                    if (isset($rw_settings[$rclass]) && is_array($rw_settings[$rclass]) && !isset($rw_settings[$rclass]["enabled"])) {
                        if (RWLogger::IsOn()) {
                            RWLogger::Log('rw_attach_rating_js', 'Class = ' . $rclass . ';');
                        }
                        // Forum reply should have exact same settings as forum post.
                        $alias = 'forum-reply' === $rclass ? 'forum-post' : $rclass;
                        $rw_settings[$rclass]["enabled"] = true;
                        // Get rating front posts settings.
                        $rw_settings[$rclass]["options"] = $this->GetOption($rw_settings[$rclass]["options"]);
                        /*
                         * We don't want to display the number of votes when the comment rating mode is "Review" or "Admin-only ratings".
                         * So we're modifying the rating label so that it will be based on the vote. e.g.: 5-star vote = "Excellent".
                         */
                        if ('comment' === $rclass && ($this->is_comment_review_mode() || $this->is_comment_admin_ratings_mode())) {
                            $options = $rw_settings[$rclass]["options"];
                            if (!isset($options->label)) {
                                $options->label = new stdClass();
                            }
                            if (!isset($options->label->text)) {
                                $options->label->text = new stdClass();
                            }
                            if (!isset($options->label->text->star)) {
                                $options->label->text->star = new stdClass();
                            }
                            if (!isset($options->label->text->nero)) {
                                $options->label->text->nero = new stdClass();
                            }
                            /**
                             * The following will show the same label when the rating is not empty whether the viewer has already voted or has not voted yet.
                             *
                             * e.g.: Instead of showing "Rate this (2 Votes)" or "5 Votes", the label will be "Excellent", "Good", or "Awful", depending on the label settings.
                             */
                            $options->label->text->star->normal = '{{rating.lastVote}}';
                            $options->label->text->star->rated = '{{rating.lastVote}}';
                            $options->label->text->nero->rated = '{{rating.lastVote}}';
                            $options->label->text->nero->normal = '{{text.rateThis}}';
                            $options->showToolip = false;
                            $options->showReport = false;
                        }
                        if (WP_RW__AVAILABILITY_DISABLED === $this->rw_validate_availability($alias)) {
                            // Disable ratings (set them to be readOnly).
                            $rw_settings[$rclass]["options"]->readOnly = true;
                        }
                        $attach_js = true;
                    }
                }
            }
            $is_bp_activity_component = function_exists('bp_is_activity_component') && bp_is_activity_component();
            if (!$attach_js) {
                // Necessary for rendering newly inserted activity ratings
                // when the are no status updates or comments yet
                if ($is_bp_activity_component) {
                    $bp_rclasses = array('activity-update', 'activity-comment');
                    foreach ($bp_rclasses as $rclass) {
                        if (isset($rw_settings[$rclass]) && !isset($rw_settings[$rclass]["enabled"])) {
                            if (RWLogger::IsOn()) {
                                RWLogger::Log('rw_attach_rating_js', 'Class = ' . $rclass . ';');
                            }
                            $rw_settings[$rclass]["enabled"] = true;
                            // Get rating class settings.
                            $rw_settings[$rclass]["options"] = $this->GetOption($rw_settings[$rclass]["options"]);
                            if (WP_RW__AVAILABILITY_DISABLED === $this->rw_validate_availability($rclass)) {
                                // Disable ratings (set them to be readOnly).
                                $rw_settings[$rclass]["options"]->readOnly = true;
                            }
                            $attach_js = true;
                        }
                    }
                }
            }
            if ($attach_js || $this->_TOP_RATED_WIDGET_LOADED) {
                ?>
					<!-- This site's ratings are powered by RatingWidget plugin v<?php 
                echo WP_RW__VERSION . ' (' . ($this->fs->is_premium() ? 'Premium' : 'Free') . ' version)';
                ?>
 - https://rating-widget.com/wordpress-plugin/ -->
					<div class="rw-js-container">
						<?php 
                if (rw_fs()->has_installed_addons()) {
                    rw_wf()->print_site_script();
                }
                ?>
						<script type="text/javascript">
							var defaultRateCallbacks = {};

							// Initialize ratings.
							function RW_Async_Init() {
								RW.init({<?php 
                // User key (uid).
                echo 'uid: "' . $this->account->site_public_key . '"';
                // User id (huid).
                if ($this->account->has_site_id()) {
                    echo ', huid: "' . $this->account->site_id . '"';
                }
                global $pagenow;
                $vid = 0;
                // Only set the vid to 1 if the comment ratings mode is set to "Admin ratings only".
                if ('comment.php' === $pagenow && $this->is_comment_admin_ratings_mode()) {
                    $vid = 1;
                } else {
                    // User logged-in.
                    $user = wp_get_current_user();
                    $vid = $user->ID;
                }
                if ($vid !== 0) {
                    // Set voter id to logged user id.
                    echo ", vid: {$vid}";
                }
                ?>
,
									source: "wordpress",
									options: {
									<?php 
                if ($this->fs->is_plan_or_trial__premium_only('professional')) {
                    if (defined('ICL_LANGUAGE_CODE') && isset($this->languages[ICL_LANGUAGE_CODE])) {
                        ?>
									lng: "<?php 
                        echo ICL_LANGUAGE_CODE;
                        ?>
"
									<?php 
                    } elseif (function_exists('pll_current_language')) {
                        ?>
									lng: "<?php 
                        echo pll_current_language();
                        ?>
"
									<?php 
                    }
                    ?>
									<?php 
                }
                ?>
								},
								identifyBy: "<?php 
                echo $this->GetOption(WP_RW__IDENTIFY_BY);
                ?>
"
							});
							<?php 
                foreach ($rw_settings as $rclass => $options) {
                    $criteria_class = $rclass;
                    $suffix_pos = strpos($rclass, $criteria_suffix_part);
                    if (false !== $suffix_pos) {
                        $rclass = substr($rclass, 0, $suffix_pos);
                    }
                    if (isset($rw_settings[$rclass]["enabled"]) && true === $rw_settings[$rclass]["enabled"]) {
                        $alias = 'forum-reply' === $rclass ? 'forum-post' : $rclass;
                        ?>
							var options = <?php 
                        echo !empty($rw_settings[$alias]["options"]) ? json_encode($rw_settings[$rclass]["options"]) : '{}';
                        ?>
;
							<?php 
                        echo $this->GetCustomSettings($alias);
                        ?>
							<?php 
                        if (rw_fs()->has_installed_addons()) {
                            ?>
							defaultRateCallbacks['<?php 
                            echo $rclass;
                            ?>
'] = {
								'afterRate' : options.afterRate ? options.afterRate : false,
								'beforeRate': options.beforeRate ? options.beforeRate : false
							};

							options.beforeRate = function (rating, score) {
								var returnValue = true;
								if (false !== defaultRateCallbacks['<?php 
                            echo $rclass;
                            ?>
'].beforeRate) {
									returnValue = defaultRateCallbacks['<?php 
                            echo $rclass;
                            ?>
'].beforeRate(rating, score);
								}

								return WF_Engine.eval('beforeVote', rating, score, returnValue);
							};

							options.afterRate = function (success, score, rating) {
								if (false !== defaultRateCallbacks['<?php 
                            echo $rclass;
                            ?>
'].afterRate) {
									defaultRateCallbacks['<?php 
                            echo $rclass;
                            ?>
'].afterRate(success, score, rating);
								}

								WF_Engine.eval('afterVote', rating, score);

								return true;
							};
							<?php 
                        }
                        ?>

							RW.initClass("<?php 
                        echo $criteria_class;
                        ?>
", options);
							<?php 
                    }
                }
                foreach (self::$ratings as $urid => $data) {
                    if (is_string($data["title"]) && !empty($data["title"]) || is_string($data["permalink"]) && !empty($data["permalink"]) || isset($data["img"])) {
                        $properties = array();
                        if (is_string($data["title"]) && !empty($data["title"])) {
                            $properties[] = 'title: ' . json_encode(esc_js($data["title"]));
                        }
                        if (is_string($data["permalink"]) && !empty($data["permalink"])) {
                            $properties[] = 'url: ' . json_encode(esc_js($data["permalink"]));
                        }
                        if (isset($data["img"])) {
                            $properties[] = 'img: ' . json_encode(esc_js($data["img"]));
                        }
                        echo 'RW.initRating("' . $urid . '", {' . implode(', ', $properties) . '});';
                    }
                }
                ?>
							RW.render(function () {
								(function ($) {
									$('.rw-rating-table:not(.rw-no-labels):not(.rw-comment-admin-rating)').each(function () {
										var ratingTable = $(this);

										// Find the current width before floating left or right to
										// keep the ratings aligned
										var col1 = ratingTable.find('td:first');
										var widthCol1 = col1.width();
										ratingTable.find('td:first-child').width(widthCol1);

										if (ratingTable.hasClass('rw-rtl')) {
											ratingTable.find('td').css({float: 'right'});
										} else {
											ratingTable.find('td').css({float: 'left'});
										}
									});
								})(jQuery);
							}, <?php 
                echo !$this->_TOP_RATED_WIDGET_LOADED ? "true" : "false";
                ?>
);
							}

							RW_Advanced_Options = {
								blockFlash: !(<?php 
                $flash = $this->GetOption(WP_RW__FLASH_DEPENDENCY, true);
                echo in_array($flash, array('true', 'false')) ? $flash : (false === $flash ? 'false' : 'true');
                ?>
)
							};

							// Append RW JS lib.
							if (typeof(RW) == "undefined") {
								(function () {
									var rw = document.createElement("script");
									rw.type = "text/javascript";
									rw.async = true;
									rw.src = "<?php 
                echo rw_get_js_url('external' . (!WP_RW__DEBUG ? '.min' : '') . '.php');
                ?>
?wp=<?php 
                echo WP_RW__VERSION;
                ?>
";
									var s = document.getElementsByTagName("script")[0];
									s.parentNode.insertBefore(rw, s);
								})();
							}
						</script>
					</div>
					<!-- / RatingWidget plugin -->
					<?php 
                // Enqueue the script that will handle the rendering
                // of the rating of the newly inserted BuddyPress status update
                // or comment
                if ($is_bp_activity_component) {
                    rw_enqueue_script('rw-site-ajax-handler', WP_RW__PLUGIN_URL . 'resources/js/site-ajax-handler.js');
                }
            }
        }