defaultsFieldsNames = [ wpdFormConst::WPDISCUZ_FORMS_NAME_FIELD, wpdFormConst::WPDISCUZ_FORMS_EMAIL_FIELD, wpdFormConst::WPDISCUZ_FORMS_WEBSITE_FIELD, wpdFormConst::WPDISCUZ_FORMS_CAPTCHA_FIELD, wpdFormConst::WPDISCUZ_FORMS_SUBMIT_FIELD]; $this->wpdOptions = $options; $this->setFormID($formID); $this->row = new Row(); $this->captchaFied = Captcha::getInstance(); } public function initFormMeta() { if ((int)$this->formID) { if (!$this->generalOptions) { $goptions = get_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS, true); $this->generalOptions = $goptions ? $goptions : []; } if (!$this->formeStructure) { $this->formeStructure = get_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_STRUCTURE, true); } } if (!$this->formPostTypes) { $this->formPostTypes = isset($this->generalOptions[wpdFormConst::WPDISCUZ_META_FORMS_POSTE_TYPES]) ? $this->generalOptions[wpdFormConst::WPDISCUZ_META_FORMS_POSTE_TYPES] : []; } } public function initFormFields() { if (!$this->formFields) { $this->formCustomFields = []; $this->formFields = get_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_FIELDS, true); if (is_array($this->formFields)) { foreach ($this->formFields as $key => $field) { if (is_callable($field["type"] . "::getInstance") && !in_array($key, $this->defaultsFieldsNames)) { if (!empty($field["icon"])) { $this->hasIcon = true; } $this->formCustomFields[$key] = $field; if ($field["type"] === "wpdFormAttr\Field\RatingField") { $this->ratingsFieldsKey[] = $key; } } } } if (count($this->ratingsFieldsKey)) { $this->ratingsExists = true; } } } public function getFormCustomFields() { return $this->formCustomFields; } public function setFormID($formID) { if ($formID == 0) { $this->formID = $formID; return; } $form = get_post($formID); if ($form && $form->post_type === wpdFormConst::WPDISCUZ_FORMS_CONTENT_TYPE) { $this->formID = $formID; do_action("wpdiscuz_form_init", $this); } else { $postRel = $this->wpdOptions->formPostRel; $contentRel = $this->wpdOptions->formContentTypeRel; foreach ($postRel as $pid => $fid) { if ($formID == $fid) { unset($postRel[$pid]); } } foreach ($contentRel as $postType => $postTypeData) { foreach ($postTypeData as $lang => $fid) { if ($formID == $fid) { unset($contentRel[$postType][$lang]); } } } update_option(wpdFormConst::WPDISCUZ_FORMS_CONTENT_TYPE_REL, $contentRel); update_option(wpdFormConst::WPDISCUZ_FORMS_POST_REL, $postRel); $this->formID = 0; } } public function getFormID() { return $this->formID; } public function getGeneralOptions() { return $this->generalOptions; } public function getHeaderTextSingle() { $this->initFormMeta(); if (!isset($this->generalOptions["header_text_single"])) { $this->generalOptions["header_text_single"] = esc_html__("Comment", "wpdiscuz"); } return $this->generalOptions["header_text_single"]; } public function getHeaderTextPlural() { $this->initFormMeta(); if (empty($this->generalOptions["header_text_plural"])) { $this->generalOptions["header_text_plural"] = esc_html__("Comments", "wpdiscuz"); } return $this->generalOptions["header_text_plural"]; } public function getTheme() { $this->initFormMeta(); if (empty($this->generalOptions["theme"])) { $this->generalOptions["theme"] = $this->getDefaultTheme(); } else { if (!is_dir($this->generalOptions["theme"])) { $themeName = wp_basename($this->generalOptions["theme"]); if (strpos($this->generalOptions["theme"], "plugins") === false) { $uplDir = wp_upload_dir(); $themesDir = str_replace("\\", "/", $uplDir["basedir"]) . wpdFormConst::THEMES_DIR; if (is_dir($themesDir . $themeName)) { $this->generalOptions["theme"] = $themesDir . $themeName; } else { $this->generalOptions["theme"] = $this->getDefaultTheme(); } } else { $themeDir = str_replace("\\", "/", WPDISCUZ_DIR_PATH) . "/themes/" . $themeName; if (is_dir($themeDir)) { $this->generalOptions["theme"] = $themeDir; } else { $this->generalOptions["theme"] = $this->getDefaultTheme(); } } } } return $this->generalOptions["theme"]; } public function getLayout() { $this->initFormMeta(); if (empty($this->generalOptions["layout"])) { $this->generalOptions["layout"] = 1; } return $this->generalOptions["layout"]; } public function getEnableRateOnPost() { $this->initFormMeta(); if (!array_key_exists("enable_post_rating", $this->generalOptions)) { $this->generalOptions["enable_post_rating"] = 1; } return (int)$this->generalOptions["enable_post_rating"]; } public function getPostRatingTitle() { $this->initFormMeta(); if (!isset($this->generalOptions["post_rating_title"])) { $this->generalOptions["post_rating_title"] = esc_html__("Article Rating", "wpdiscuz"); } return $this->generalOptions["post_rating_title"]; } public function getRatingsExists() { $this->initFormMeta(); $this->initFormFields(); return $this->ratingsExists; } public function getUserCanRateOnPost() { $this->initFormMeta(); if (!isset($this->generalOptions["allow_guests_rate_on_post"])) { $this->generalOptions["allow_guests_rate_on_post"] = 1; } return $this->generalOptions["allow_guests_rate_on_post"]; } public function getCaptchaFied() { return $this->captchaFied; } public function isShowSubscriptionBar() { return $this->generalOptions["show_subscription_bar"]; } public function isShowSubscriptionBarAgreement() { $this->initFormMeta(); return isset($this->generalOptions["show_subscription_agreement"]) ? $this->generalOptions["show_subscription_agreement"] : 0; } public function subscriptionBarAgreementLabel() { return isset($this->generalOptions["subscription_agreement_label"]) ? $this->generalOptions["subscription_agreement_label"] : esc_html__("I allow to use my email address and send notification about new comments and replies (you can unsubscribe at any time).", "wpdiscuz"); } public function getCustomCSS() { return get_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_CSS, true); } public function getFormPostTypes() { return $this->formPostTypes; } public function getFormFields() { return $this->formFields; } public function theFormListData($column, $formID) { $this->setFormID($formID); $this->generalOptions = get_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS, true); if ($column === "form_post_types") { $postTypes = isset($this->generalOptions[wpdFormConst::WPDISCUZ_META_FORMS_POSTE_TYPES]) ? $this->generalOptions[wpdFormConst::WPDISCUZ_META_FORMS_POSTE_TYPES] : ""; echo $postTypes ? htmlentities(implode(", ", $this->generalOptions[wpdFormConst::WPDISCUZ_META_FORMS_POSTE_TYPES])) : ""; } else if ($column === "form_post_ids") { echo isset($this->generalOptions["postid"]) ? htmlentities($this->generalOptions["postid"]) : ""; } else if ($column === "form_lang") { echo isset($this->generalOptions["lang"]) ? htmlentities($this->generalOptions["lang"]) : ""; } } public function saveFormData($formID) { $this->setFormID($formID); $this->initFormMeta(); if (isset($_REQUEST[wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS])) { $generalOptions = $this->validateGeneralOptions($_REQUEST[wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS]); $this->saveFormContentTypeRel($generalOptions[wpdFormConst::WPDISCUZ_META_FORMS_POSTE_TYPES], $generalOptions["lang"]); $this->saveFormPostRel($generalOptions["postidsArray"]); update_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS, $generalOptions); } if (isset($_REQUEST[wpdFormConst::WPDISCUZ_META_FORMS_STRUCTURE])) { $formeStructure = $this->validateFormStructure($_REQUEST[wpdFormConst::WPDISCUZ_META_FORMS_STRUCTURE]); update_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_STRUCTURE, $formeStructure); update_post_meta($this->formID, wpdFormConst::WPDISCUZ_META_FORMS_FIELDS, $this->formFields); } } public function saveCommentMeta($commentID) { $comment = get_comment($commentID); $commentApproved = $comment->comment_approved; do_action("wpdiscuz_before_save_commentmeta", $comment, $this->fieldsBeforeSave); foreach ($this->fieldsBeforeSave as $mettaKey => $data) { if ($this->ratingsExists && $this->formCustomFields[$mettaKey]["type"] === "wpdFormAttr\Field\RatingField") { $oldCommentRating = get_comment_meta($commentID, $mettaKey, true); $postID = $comment->comment_post_ID; if (!(class_exists("WooCommerce") && get_post_type($postID) === "product") && $oldCommentRating && $commentApproved === "1") { $postRatingMeta = get_post_meta($postID, wpdFormConst::WPDISCUZ_RATING_COUNT, true); $postRatingMeta = is_array($postRatingMeta) ? $postRatingMeta : []; $oldCommentRatingCount = $postRatingMeta[$mettaKey][$oldCommentRating] - 1; if ($oldCommentRatingCount > 0) { $postRatingMeta[$mettaKey][$oldCommentRating] = $oldCommentRatingCount; } else { unset($postRatingMeta[$mettaKey][$oldCommentRating]); } update_post_meta($postID, wpdFormConst::WPDISCUZ_RATING_COUNT, $postRatingMeta); $this->updateSeparateRatingMeta($postRatingMeta, $postID); } $this->ratings[] = ["metakey" => $mettaKey, "value" => $data]; } update_comment_meta($commentID, $mettaKey, $data); } if ($this->ratingsExists && $this->ratings) { $ratingSum = 0; foreach ($this->ratings as $k => $rating) { $ratingSum += (int)$rating["value"]; } $gRating = round($ratingSum / count($this->ratings)); update_comment_meta($commentID, "rating", $gRating); if ($commentApproved === "1") { $this->savePostRatingMeta($comment, $gRating); } } } private function savePostRatingMeta($comment, $rating) { $postID = $comment->comment_post_ID; if (class_exists("WooCommerce") && get_post_type($postID) === "product") { $ratingCount = get_post_meta($postID, "_wc_rating_count", true); $ratingCount = is_string($ratingCount) ? [] : $ratingCount; $oldRatingMeta = get_comment_meta($comment->comment_ID, "rating", true); $oldRating = $oldRatingMeta ? $oldRatingMeta : 0; if (isset($ratingCount[$oldRating])) { $oldRatingCount = $ratingCount[$oldRating] - 1; if ($oldRatingCount > 0) { $ratingCount[$oldRating] = $oldRatingCount; } else { unset($ratingCount[$oldRating]); } } if (isset($ratingCount[$rating])) { $ratingCount[$rating] = $ratingCount[$rating] + 1; } else if ($rating) { $ratingCount[$rating] = 1; } $allRatingSum = 0; $allCount = 0; foreach ($ratingCount as $star => $count) { $allRatingSum += $star * $count; $allCount += $count; } if ($allCount) { $averageRating = round($allRatingSum / $allCount, 2); update_post_meta($postID, "_wc_average_rating", $averageRating); update_post_meta($postID, "_wc_rating_count", $ratingCount); } } else { $wpdiscuzRatingCountMeta = get_post_meta($postID, wpdFormConst::WPDISCUZ_RATING_COUNT, true); $wpdiscuzRatingCount = $wpdiscuzRatingCountMeta && is_array($wpdiscuzRatingCountMeta) ? $wpdiscuzRatingCountMeta : []; $wpdiscuzRatingCount = $this->cleanUnusedData($wpdiscuzRatingCount, $this->ratings); foreach ($this->ratings as $key => $value) { if (isset($wpdiscuzRatingCount[$value["metakey"]][$value["value"]])) { $wpdiscuzRatingCount[$value["metakey"]][$value["value"]] = $wpdiscuzRatingCount[$value["metakey"]][$value["value"]] + 1; } else if ($value["value"]) { $wpdiscuzRatingCount[$value["metakey"]][$value["value"]] = 1; } } update_post_meta($postID, wpdFormConst::WPDISCUZ_RATING_COUNT, $wpdiscuzRatingCount); $this->updateSeparateRatingMeta($wpdiscuzRatingCount, $postID); } } private function cleanUnusedData($ratingMeta, $ratings) { $ratingMetaKeys = array_keys($ratingMeta); foreach ($ratingMetaKeys as $key => $ratingMetaKey) { $exists = false; foreach ($ratings as $k => $rating) { if ($rating["metakey"] === $ratingMetaKey) { $exists = true; break; } } if (!$exists) { unset($ratingMeta[$ratingMetaKey]); } } return $ratingMeta; } public function displayRatingMeta($content) { global $post; if (!(class_exists("WooCommerce") && get_post_type($post) === "product")) { $this->initFormFields(); if (in_array("before", $this->wpdOptions->rating["displayRatingOnPost"])) { if ($this->ratingsExists) { $content = $this->getRatingMetaHtml() . $content; } else if ($this->getEnableRateOnPost()) { $content = $this->getPostRatingHtml() . $content; } } if (in_array("after", $this->wpdOptions->rating["displayRatingOnPost"])) { if ($this->ratingsExists) { $content .= $this->getRatingMetaHtml(); } else if ($this->getEnableRateOnPost()) { $content .= $this->getPostRatingHtml(); } } } return $content; } public function displayRatingMetaBeforeCommentForm() { global $post; $content = ""; if (!(class_exists("WooCommerce") && get_post_type($post) === "product")) { if (in_array("before_comment_form", $this->wpdOptions->rating["displayRatingOnPost"])) { if ($this->ratingsExists) { $content = $this->getRatingMetaHtml(); } else if ($this->getEnableRateOnPost()) { $content = $this->getPostRatingHtml(); } } } echo $content; } public function getPostRatingHtml($can_rate = true) { $html = ""; $wpdiscuz = wpDiscuz(); if ($wpdiscuz->isWpdiscuzLoaded && $this->getEnableRateOnPost() && (($this->wpdOptions->rating["ratingCssOnNoneSingular"] && !is_singular()) || is_singular())) { global $post; $currentUserId = get_current_user_id(); $class = ""; if ($can_rate && is_singular()) { if (!empty($currentUserId)) { $class = wpDiscuz()->dbManager->isUserRated($currentUserId, "", $post->ID) ? "" : " class='wpd-not-rated'"; } else if ($this->getUserCanRateOnPost()) { $class = wpDiscuz()->dbManager->isUserRated(0, md5(wpDiscuz()->helper->getRealIPAddr()), $post->ID) ? "" : " class='wpd-not-rated'"; } } $rating = (float)get_post_meta($post->ID, wpdFormConst::POSTMETA_POST_RATING, true); $count = (int)get_post_meta($post->ID, wpdFormConst::POSTMETA_POST_RATING_COUNT, true); $prefix = (int)$rating; $suffix = $rating - $prefix; $fullStarSVG = apply_filters("wpdiscuz_full_star_svg", "", "post", "fas fa-star"); $halfStarSVG = apply_filters("wpdiscuz_half_star_svg", "", "post", "fas fa-star"); $html .= "
" . esc_html__("Only logged in customers who have purchased this product may leave a review.", "woocommerce") . "
"; } } $this->isUserCanComment = $user_can_comment; return $user_can_comment; } public function defaultFieldsData() { return [ "column_type" => "two", "row_order" => 0, "default" => 1, "left" => [ wpdFormConst::WPDISCUZ_FORMS_NAME_FIELD => [ "type" => "wpdFormAttr\Field\DefaultField\Name", "name" => esc_html__("Name", "wpdiscuz"), "desc" => "", "icon" => "fas fa-user", "required" => "1" ], wpdFormConst::WPDISCUZ_FORMS_EMAIL_FIELD => [ "type" => "wpdFormAttr\Field\DefaultField\Email", "name" => esc_html__("Email", "wpdiscuz"), "desc" => "", "icon" => "fas fa-at", "required" => "1" ], wpdFormConst::WPDISCUZ_FORMS_WEBSITE_FIELD => [ "type" => "wpdFormAttr\Field\DefaultField\Website", "name" => esc_html__("Website", "wpdiscuz"), "desc" => "", "icon" => "fas fa-link", "enable" => "1" ], ], "right" => [ wpdFormConst::WPDISCUZ_FORMS_CAPTCHA_FIELD => [ "type" => "wpdFormAttr\Field\DefaultField\Captcha", "name" => "", "desc" => "", "show_for_guests" => "0", "show_for_users" => "0" ], wpdFormConst::WPDISCUZ_FORMS_SUBMIT_FIELD => [ "type" => "wpdFormAttr\Field\DefaultField\Submit", "name" => esc_html__("Post Comment", "wpdiscuz") ], ], ]; } private function saveFormContentTypeRel($data, $lang) { $contentType = get_option(wpdFormConst::WPDISCUZ_FORMS_CONTENT_TYPE_REL, []); foreach ($this->formPostTypes as $k => $formPostType) { if (!in_array($formPostType, $data)) { unset($contentType[$formPostType][$lang]); } } foreach ($data as $type => $lable) { if (isset($contentType[$type][$lang]) && $contentType[$type][$lang]) { $existsFormID = $contentType[$type][$lang]; $generalOptions = get_post_meta($existsFormID, wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS, true); if (!empty($generalOptions)) { unset($generalOptions[wpdFormConst::WPDISCUZ_META_FORMS_POSTE_TYPES][$type]); } update_post_meta($existsFormID, wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS, $generalOptions); } $contentType[$type][$lang] = $this->formID; } update_option(wpdFormConst::WPDISCUZ_FORMS_CONTENT_TYPE_REL, $contentType); } private function saveFormPostRel($data) { $formPostIds = isset($this->generalOptions["postidsArray"]) ? $this->generalOptions["postidsArray"] : []; $ids = get_option(wpdFormConst::WPDISCUZ_FORMS_POST_REL, []); foreach ($formPostIds as $k => $formPostId) { if (!in_array($formPostId, $data)) { unset($ids[$formPostId]); } } foreach ($data as $k1 => $id) { if (isset($ids[$id]) && $ids[$id]) { $existsFormID = $ids[$id]; $generalOptions = get_post_meta($existsFormID, wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS, true); if (!$generalOptions) { $generalOptions = ["postidsArray" => []]; } foreach ($generalOptions["postidsArray"] as $key => $pid) { if ($pid == $id) { unset($generalOptions["postidsArray"][$key]); } } $generalOptions["postid"] = implode(", ", $generalOptions["postidsArray"]); update_post_meta($existsFormID, wpdFormConst::WPDISCUZ_META_FORMS_GENERAL_OPTIONS, $generalOptions); } $ids[$id] = $this->formID; } update_option(wpdFormConst::WPDISCUZ_FORMS_POST_REL, $ids); } public function transferJSData($data) { $this->initFormFields(); $data["is_email_field_required"] = empty($this->formFields[wpdFormConst::WPDISCUZ_FORMS_EMAIL_FIELD]["required"]) ? 0 : $this->formFields[wpdFormConst::WPDISCUZ_FORMS_EMAIL_FIELD]["required"]; return $data; } public function showRecaptcha() { return ($this->wpdOptions->recaptcha["showForGuests"] && !is_user_logged_in()) || ($this->wpdOptions->recaptcha["showForUsers"] && is_user_logged_in()) || ($this->wpdOptions->recaptcha["isShowOnSubscribeForm"] && !is_user_logged_in()); } public function customFieldsExists() { $this->initFormFields(); return $this->formCustomFields ? true : false; } public function resetData() { $this->formID = 0; $this->generalOptions = []; $this->formCustomFields = []; $this->formFields = []; } public function getLayouts($theme = '') { $theme = $theme ? $theme : $this->generalOptions["theme"]; $layouts = []; $path = $theme . "/layouts/"; $scannedLayouts = scandir($path); unset($scannedLayouts[0]); unset($scannedLayouts[1]); foreach ($scannedLayouts as $k => $scannedLayout) { if (is_dir($path . $scannedLayout)) { $layouts[] = $scannedLayout; } } return $layouts; } public function getThemes() { $themes = []; $path = str_replace("\\", "/", WPDISCUZ_DIR_PATH) . "/themes/"; $scannedThemes = scandir($path); unset($scannedThemes[0]); unset($scannedThemes[1]); foreach ($scannedThemes as $k => $scannedTheme) { if (is_dir($path . $scannedTheme)) { $theme = wp_get_theme($scannedTheme, $path); if ($theme->exists()) { $themes[$path . $scannedTheme] = [ "name" => $theme->get("Name"), "desc" => $theme->get("Description"), "url" => plugins_url(WPDISCUZ_DIR_NAME . "/themes/$scannedTheme"), ]; } } } $uplDir = wp_upload_dir(); $directory = dirname( __FILE__ ); $themesDir = str_replace("\\", "/", $directory) . wpdFormConst::THEMES_DIR; if (is_dir($themesDir)) { $scannedThemes = scandir($themesDir); unset($scannedThemes[0]); unset($scannedThemes[1]); if ( ! empty( $scannedThemes ) ) { foreach ($scannedThemes as $k => $scannedTheme) { if (is_dir($themesDir . $scannedTheme)) { $theme = wp_get_theme($scannedTheme, $themesDir); if ($theme->exists()) { $themes[$themesDir . $scannedTheme] = [ "name" => $theme->get("Name"), "desc" => $theme->get("Description"), "url" => $uplDir["baseurl"] . wpdFormConst::THEMES_DIR . $scannedTheme, ]; } } }} } return $themes; } public function getDefaultTheme() { $path = str_replace("\\", "/", WPDISCUZ_DIR_PATH) . "/themes/"; $scannedThemes = scandir($path); unset($scannedThemes[0]); unset($scannedThemes[1]); foreach ($scannedThemes as $k => $scannedTheme) { if (is_dir($path . $scannedTheme)) { $theme = wp_get_theme($scannedTheme, $path); if ($theme->exists()) { return $path . $scannedTheme; } } } } public function getAllowedFieldsType() { return $this->row->allowedFieldsType(); } public function updateSeparateRatingMeta($ratings, $post_id) { foreach ($ratings as $key => $values) { $avg = 0; $c = 0; foreach ($values as $rating => $count) { $avg += $rating * $count; $c += $count; } update_post_meta($post_id, wpdFormConst::WPDISCUZ_RATING_SEPARATE_AVG . $key, ($c === 0 ? 0 : round($avg / $c, 1))); update_post_meta($post_id, wpdFormConst::WPDISCUZ_RATING_SEPARATE_COUNT . $key, $c); } } }