config.php 25.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
<?php

namespace uncanny_learndash_toolkit;

/**
 * Class Config
 *
 * @package uncanny_learndash_toolkit
 */
class Config {
	/**
	 * @var array
	 */
	private static $available_plugins;

	/**
	 * @return array of class names
	 */
	public static function get_active_classes() {
		if ( ! self::$available_plugins ) {
			self::$available_plugins = get_option( 'uncanny_toolkit_active_classes', array() );
			if ( empty( self::$available_plugins ) ) {
				self::$available_plugins = array();
			}
		}

		return self::$available_plugins;
	}

	/**
	 * @return bool
	 */
	public static function is_pro_active() {
		include_once ABSPATH . 'wp-admin/includes/plugin.php';
		if ( function_exists( 'is_plugin_active' ) ) {
			return is_plugin_active( 'uncanny-toolkit-pro/uncanny-toolkit-pro.php' );
		}

		return defined( 'UNCANNY_TOOLKIT_PRO_VERSION' );
	}

	/**
	 * Check if the module is active in Free / Pro of Toolkit
	 *
	 * @param       $module
	 * @param false $in_pro
	 *
	 * @return bool
	 */
	public static function is_toolkit_module_active( $module, $in_pro = false ) {
		if ( $in_pro && ! self::is_pro_active() ) {
			return false;
		}

		$active_classes = self::get_active_classes();

		return in_array( $module, $active_classes, false );
	}

	/**
	 * @param string $file_name
	 *
	 * @return string
	 */
	public static function get_admin_media( $file_name ) {
		return plugins_url( 'assets/backend/img/' . $file_name, __FILE__ );
	}

	/**
	 * @param string $file_name
	 *
	 * @return string
	 */
	public static function get_admin_css( $file_name ) {
		return plugins_url( 'assets/backend/css/' . $file_name, __FILE__ );
	}

	/**
	 * @param string $file_name
	 *
	 * @return string
	 */
	public static function get_admin_js( $file_name ) {
		return plugins_url( 'assets/backend/js/' . $file_name, __FILE__ );
	}

	/**
	 * @param string $file_name
	 *
	 * @return string
	 */
	public static function get_vendor( $file_name ) {
		return plugins_url( 'assets/vendor/' . $file_name, __FILE__ );
	}

	/**
	 * @param string $file_name File name must be prefixed with a \ (foreword slash)
	 * @param mixed $file (false || __FILE__ )
	 *
	 * @return string
	 */
	public static function get_template( $file_name, $file = false ) {

		if ( ! $file ) {
			$file = __FILE__;
		}
		$template_path = apply_filters( 'uncanny_toolkit_template_path', 'uncanny-toolkit' . DIRECTORY_SEPARATOR, $file_name );
		$asset_uri     = self::locate_template( $template_path . $file_name );

		if ( empty( $asset_uri ) ) {
			$asset_uri = dirname( $file ) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $file_name;
		}

		return $asset_uri;
	}

	/**
	 * Retrieve the name of the highest priority template file that exists.
	 *
	 * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
	 * so that themes which inherit from a parent theme can just overload one file.
	 *
	 * @param string|array $template_names Template file(s) to search for, in order.
	 *
	 * @return string The template filename if one is located.
	 * @since 3.1
	 */
	public static function locate_template( $template_names ) {
		$located = '';
		foreach ( (array) $template_names as $template_name ) {
			if ( ! $template_name ) {
				continue;
			}
			if ( file_exists( $template_name ) ) {
				$located = $template_name;
				break;
			}
			if ( file_exists( get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_name ) ) {
				$located = get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_name;
				break;
			}

			if ( file_exists( get_template_directory() . DIRECTORY_SEPARATOR . $template_name ) ) {
				$located = get_template_directory() . DIRECTORY_SEPARATOR . $template_name;
				break;
			}

			if ( file_exists( ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'theme-compat' . DIRECTORY_SEPARATOR . $template_name ) ) {
				$located = ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'theme-compat' . DIRECTORY_SEPARATOR . $template_name;
				break;
			}
		}

		return $located;
	}

	/**
	 * @param string $file_name File name must be prefixed with a \ (foreword slash)
	 * @param mixed $file (false || __FILE__ )
	 *
	 * @return string
	 */
	public static function get_include( $file_name, $file = false ) {

		if ( ! $file ) {
			$file = __FILE__;
		}

		$asset_uri = dirname( $file ) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . $file_name;

		return $asset_uri;
	}

	/**
	 * @return string
	 */
	public static function get_namespace() {
		return __NAMESPACE__;
	}

	/**
	 * @param array $array Array where there is slashes in the key
	 *
	 * @return array
	 */
	public static function stripslashes_deep( $array ) {
		$new_array = array();

		// strip slashes of all keys in array
		foreach ( $array as $key => $content ) {
			$key               = stripslashes( $key );
			$new_array[ $key ] = $content;
		}

		return $new_array;
	}

	/**
	 * Loops through array of setting values and return an link and settings html
	 *
	 * @param array $settings
	 *
	 * @return array
	 */
	public static function settings_output( $settings ) {

		$class   = $settings['class'];// define by __CLASS__ from related php file
		$title   = $settings['title'];
		$options = $settings['options'];

		$modal_id = stripslashes( $class );
		$modal_id = str_replace( __NAMESPACE__, '', $modal_id );

		add_filter(
			'tiny_mce_before_init',
			function ( $init ) {
				$init['extended_valid_elements'] = '*[*]';
				$init['remove_linebreaks']       = false;
				$init['convert_newlines_to_brs'] = true;
				$init['remove_redundant_brs']    = false;

				return $init;
			}
		);
		ob_start();

		?>

		<div class="ult-modal" data-settings="<?php echo $modal_id; ?>">
			<div class="ult-modal-box">
				<div class="ult-modal__header">
					<div class="ult-modal-title">
						<div class="ult-modal-title__icon"></div>
						<div class="ult-modal-title__text">
							<?php echo $title; ?>
						</div>
					</div>
				</div>
				<form method="POST" class="ult-modal-form ult-modal-form-js">
					<div class="ult-modal-options">
						<?php

						// Create options
						foreach ( $options as $content ) {

							// Show if
							$show_if = isset( $content['show_if'] ) && is_array( $content['show_if'] ) ? $content['show_if'] : '';
							$show_if = is_array( $show_if ) ? esc_attr( wp_json_encode( $show_if ) ) : '';

							// Default value
							$default_value = isset( $content['default'] ) ? $content['default'] : '';

							// If it has a "show if" defined, hide it
							$css_class = isset( $content['class'] ) ? $content['class'] : '';
							if ( ! empty( $show_if ) ) {
								$css_class = $css_class . ' ult-modal-form-row--hide';
							}

							$placeholder     = isset( $content['placeholder'] ) ? $content['placeholder'] : '';
							$inner_html      = isset( $content['inner_html'] ) ? $content['inner_html'] : '';
							$option_name     = isset( $content['option_name'] ) ? $content['option_name'] : '';
							$label           = isset( $content['label'] ) ? $content['label'] : '';
							$description     = isset( $content['description'] ) ? $content['description'] : '';
							$select_name     = isset( $content['select_name'] ) ? $content['select_name'] : '';
							$content_options = isset( $content['options'] ) ? $content['options'] : array();
							$radio_name      = isset( $content['radio_name'] ) ? $content['radio_name'] : '';
							$radio_options   = isset( $content['radios'] ) ? $content['radios'] : array();

							switch ( $content['type'] ) {
								case 'html':
									?>
									<div
										class="ult-modal-form-row ult-modal-form-row--html <?php echo $css_class; ?>"
										data-show-if="<?php echo $show_if; ?>"
										data-type="html"
									>
										<?php echo $inner_html; ?>
									</div>
									<?php
									break;
								case 'text':
									?>
									<div
										class="ult-modal-form-row ult-modal__field--text <?php echo $css_class; ?>"
										data-show-if="<?php echo $show_if; ?>"
										data-default="<?php echo $default_value; ?>"
										data-id="<?php echo $option_name; ?>"
										data-type="text"
									>
										<div class="ult-modal-form-row__label">
											<?php echo $label; ?>
										</div>
										<div class="ult-modal-form-row__field">
											<input type="text" placeholder="<?php echo $placeholder; ?>"
												   class="ult-modal-form-row__input"
												   name="<?php echo $option_name; ?>" data-type="text">

											<?php if ( ! empty( $description ) ) { ?>
												<div class="ult-modal-form-row__description">
													<?php echo $description; ?>
												</div>
											<?php } ?>
										</div>
									</div>

									<?php

									break;

								case 'number':
									?>
									<div
										class="ult-modal-form-row ult-modal__field--text <?php echo $css_class; ?>"
										data-show-if="<?php echo $show_if; ?>"
										data-default="<?php echo $default_value; ?>"
										data-id="<?php echo $option_name; ?>"
										data-type="text"
									>
										<div class="ult-modal-form-row__label">
											<?php echo $label; ?>
										</div>
										<div class="ult-modal-form-row__field">
											<input type="number" placeholder="<?php echo $placeholder; ?>"
												   class="ult-modal-form-row__input"
												   name="<?php echo $option_name; ?>" data-type="number" min="0">

											<?php if ( ! empty( $description ) ) { ?>
												<div class="ult-modal-form-row__description">
													<?php echo $description; ?>
												</div>
											<?php } ?>
										</div>
									</div>

									<?php

									break;

								

								case 'color':
									?>
									<div
										class="ult-modal-form-row ult-modal__field--color <?php echo $css_class; ?>"
										data-show-if="<?php echo $show_if; ?>"
										data-default="<?php echo $default_value; ?>"
										data-type="color"
										data-id="<?php echo $option_name; ?>"
									>
										<div class="ult-modal-form-row__label">
											<?php echo $label; ?>
										</div>
										<div class="ult-modal-form-row__field">
											<input type="color"
												   placeholder="<?php echo $placeholder; ?>"
												   class="ult-modal-form-row__color uo-color-picker"
												   name="<?php echo $option_name; ?>"
												   data-type="color"
											>

											<?php if ( ! empty( $description ) ) { ?>
												<div class="ult-modal-form-row__description">
													<?php echo $description; ?>
												</div>
											<?php } ?>
										</div>
									</div>
									<?php
									break;
								case 'textarea':
									//Fallback method for old toolkit
									if ( version_compare( UNCANNY_TOOLKIT_VERSION, '2.4' ) >= 0 ) {
										// TinyMCE.

										$tinymce_content = self::get_settings_value( $option_name, $class );

										if ( empty( $tinymce_content ) ) {
											$tinymce_content = $placeholder;
										}

										$tinymce_content = stripslashes( $tinymce_content );

										?>

										<div
											class="ult-modal-form-row ult-modal__field--tinymce <?php echo $css_class; ?>"
											data-show-if="<?php echo $show_if; ?>"
											data-default="<?php echo $default_value; ?>"
											data-type="textarea"
											data-id="<?php echo $option_name; ?>"
										>
											<div class="ult-modal-form-row__label">
												<?php echo $label; ?>
											</div>
											<div class="ult-modal-form-row__field">
												<?php

												echo wp_editor(
													$tinymce_content,
													$option_name,
													array(
														'editor_class'  => 'ult-tinymce',
														'media_buttons' => false,
														'editor_height' => 275,
													)
												);
												?>

												<?php if ( ! empty( $description ) ) { ?>
													<div class="ult-modal-form-row__description">
														<?php echo $description; ?>
													</div>
												<?php } ?>
											</div>
										</div>

										<?php
									} else {
										?>
										<div
											class="ult-modal-form-row ult-modal__field--textarea <?php echo $css_class; ?>"
											data-show-if="<?php echo $show_if; ?>"
											data-default="<?php echo $default_value; ?>"
											data-type="textarea"
											data-id="<?php echo $option_name; ?>"
										>
											<div class="ult-modal-form-row__label">
												<?php echo $label; ?>
											</div>
											<div class="ult-modal-form-row__field">
												<textarea
													class="ult-modal-form-row__textarea"
													name="<?php echo $option_name; ?>"
													placeholder="<?php echo $placeholder; ?>"
													type="textarea"></textarea>

												<?php if ( ! empty( $description ) ) { ?>
													<div class="ult-modal-form-row__description">
														<?php echo $description; ?>
													</div>
												<?php } ?>
											</div>
										</div>

										<?php
									}

									break;
								case 'checkbox':
									?>
									<div
										class="ult-modal-form-row ult-modal__field--checkbox <?php echo $css_class; ?>"
										data-show-if="<?php echo $show_if; ?>"
										data-default="<?php echo $default_value; ?>"
										data-type="checkbox"
										data-id="<?php echo $option_name; ?>"
									>
										<div class="ult-modal-form-row__field">
											<label>
												<input type="checkbox" name="<?php echo $option_name; ?>"
													   class="ult-modal-form-row__checkbox" data-type="checkbox">
												<?php echo $label; ?>
											</label>

											<?php if ( ! empty( $description ) ) { ?>
												<div class="ult-modal-form-row__description">
													<?php echo $description; ?>
												</div>
											<?php } ?>
										</div>
									</div>

									<?php
									break;
								case 'radio';
									?>
									<div
										class="ult-modal-form-row ult-modal__field--radio <?php echo $css_class; ?>"
										data-show-if="<?php echo $show_if; ?>"
										data-default="<?php echo $default_value; ?>"
										data-type="radio"
										data-id="<?php echo $radio_name; ?>"
									>
										<div class="ult-modal-form-row__label">
											<?php echo $label; ?>
										</div>
										<div class="ult-modal-form-row__field">
											<?php

											/**
											 * This type of field has an exception. We're expecting more than one input radio
											 */
											if ( $radio_options ) {
												foreach ( $radio_options as $radio ) {
													?>

													<label class="ult-modal-form-row__radio-label">
														<input type="radio" name="<?php echo $radio_name; ?>"
															   value="<?php echo $radio['value']; ?>" data-type="radio">
														<span>
														<?php echo $radio['text']; ?>
													</span>
													</label>

													<?php
												}
											}

											?>

											<?php if ( ! empty( $description ) ) { ?>
												<div class="ult-modal-form-row__description">
													<?php echo $description; ?>
												</div>
											<?php } ?>
										</div>
									</div>

									<?php

									break;
								case 'select':
									?>
									<div
										class="ult-modal-form-row ult-modal__field--select <?php echo $css_class; ?>"
										data-show-if="<?php echo $show_if; ?>"
										data-default="<?php echo $default_value; ?>"
										data-type="select"
										data-id="<?php echo $select_name; ?>"
									>
										<div class="ult-modal-form-row__label">
											<?php echo $label; ?>
										</div>
										<div class="ult-modal-form-row__field">
											<select class="ult-modal-form-row__select"
													name="<?php echo $select_name; ?>" data-type="select">
												<?php
												if ( ! empty( $content_options ) ) {
													foreach ( $content_options as $option ) {
														?>
														<option value="<?php echo $option['value']; ?>">
															<?php echo $option['text']; ?>
														</option>
														<?php
													}
												}
												?>
											</select>

											<?php if ( ! empty( $description ) ) { ?>
												<div class="ult-modal-form-row__description">
													<?php echo $description; ?>
												</div>
											<?php } ?>
										</div>
									</div>

									<?php

									break;
							}
						}

						?>
					</div>
					<div class="ult-modal-footer">
						<div class="ult-modal-notice"></div>
						<div class="ult-modal-actions">
							<div class="ult-modal-actions__left">
								<div class="ult-modal-action">
									<a target="_blank"
									   class="ult-modal-action__btn ult-modal-action__btn--secondary ult-modal-action__btn-help-js">
										<?php _e( 'Help', 'uncanny-learndash-toolkit' ); ?>
									</a>
								</div>
							</div>
							<div class="ult-modal-actions__right">
								<div class="ult-modal-action">
									<div
										class="ult-modal-action__btn ult-modal-action__btn--secondary ult-modal-action__btn-cancel-js"
										data-action="cancel">
										<?php _e( 'Close', 'uncanny-learndash-toolkit' ); ?>
									</div>
								</div>
								<div class="ult-modal-action">
									<button
										class="ult-modal-action__btn ult-modal-action__btn--primary ult-modal-action__btn-submit-js"
										type="submit">
										<?php _e( 'Save module', 'uncanny-learndash-toolkit' ); ?>
									</button>
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>


		<?php

		$html_options = ob_get_clean();

		return array(
			'link'  => '',
			'modal' => $html_options,
		);

	}

	/**
	 *
	 */
	public static function ajax_activate_deactivate_module() {
		// Nonce verification
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uncanny_toolkit' ) ) {
			echo 'nonce failed';
			wp_die();
		}
		$capability = apply_filters( 'toolkit_settings_module_switch_cap', 'manage_options' );
		if ( ! current_user_can( $capability ) ) {
			echo __( 'You must be an admin to save settings', 'uncanny-learndash-toolkit' );
			wp_die();
		}
		if ( ! isset( $_POST['value'] ) ) {
			echo 'Value field missing.';
			wp_die();
		}
		$value          = stripslashes( $_POST['value'] );
		$active_classes = get_option( 'uncanny_toolkit_active_classes', 0 );

		if ( 0 !== $active_classes ) {
			if ( ! is_array( $active_classes ) ) {
				$active_classes = array();
			}
			if ( 'active' === $_POST['active'] ) {
				$new_classes = array_merge( array( $value => $value ), $active_classes );
			} elseif ( 'inactive' === $_POST['active'] ) {
				unset( $active_classes[ $value ] );
				$new_classes = $active_classes;
			}
			update_option( 'uncanny_toolkit_active_classes', $new_classes );
			$response = 'success';
		} else {
			$save_settings = add_option( 'uncanny_toolkit_active_classes', array( $value => $value ) );
			$response      = ( $save_settings ) ? 'success' : 'notsaved';
		}

		// If the uo dashboard module is being turned on then set the default template as 3_0
		if ( 'uncanny_pro_toolkit\\learnDashMyCourses' === $value ) {
			if ( 'active' === $_POST['active'] ) {
				update_option(
					'uncanny_pro_toolkitlearnDashMyCourses',
					array(
						array(
							'name'  => 'uo_dashboard_template',
							'value' => '3_0',
						),
					),
					'no'
				);
			}
		}

		// If the frontend login module is being turned on then check if settings are available or not.
		if ( 'uncanny_learndash_toolkit\FrontendLoginPlus' === $value ) {
			if ( 'active' === $_POST['active'] ) {
				$existing_settings = get_option( 'FrontendLoginPlus', '' );
				if ( empty( $existing_settings ) ) {
					$default_settings = array(
						array(
							'name'  => 'uo_frontendloginplus_enable_ajax_support',
							'value' => 'on',
						),
					);
					update_option( 'FrontendLoginPlus', $default_settings );
				}
			}
		}

		echo $response; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		wp_die();
	}

	/**
	 *
	 */
	public static function ajax_settings_save() {
		// Nonce verification
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uncanny_toolkit' ) ) {
			echo wp_json_encode(
				array(
					'error'   => true,
					'message' => 'Nonce verification failed.',
				)
			);
			wp_die();
		}
		$response = array(
			'error'   => true,
			'message' => '',
		);

		$capability = apply_filters( 'toolkit_settings_save_cap', 'manage_options' );

		if ( ! current_user_can( $capability ) ) {
			echo wp_json_encode(
				array(
					'error'   => true,
					'message' => __( 'You must be an admin to save settings', 'uncanny-learndash-toolkit' ),
				)
			);
			wp_die();
		}

		if ( ! isset( $_POST['class'] ) ) {
			echo wp_json_encode(
				array(
					'error'   => true,
					'message' => __( 'Class for addon is not set', 'uncanny-learndash-toolkit' ),
				)
			);
			wp_die();
		}

		$class   = sanitize_text_field( $_POST['class'] );
		$options = ( isset( $_POST['options'] ) ) ? $_POST['options'] : array();

		// Validate action if any module need some values to set.
		do_action( 'toolkit_settings_save_validation', $class, $options );

		// Delete option and add option are called instead of update option because
		// sometimes update value is equal to the existing value and a false
		// positive is returned

		delete_option( $class );

		$save_settings = add_option( $class, $options );

		$response['error'] = ! $save_settings;

		if ( $save_settings ) {
			$response['message'] = __( 'Settings saved successfully', 'uncanny-learndash-toolkit' );
		} else {
			$response['message'] = __( 'Something went wrong. Please, try again', 'uncanny-learndash-toolkit' );
		}

		echo wp_json_encode( $response );

		wp_die();

	}


	/**
	 *
	 */
	public static function ajax_settings_load() {
		// Nonce verification
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uncanny_toolkit' ) ) {
			echo 'Nonce verification failed.';
			wp_die();
		}

		$capability = apply_filters( 'toolkit_settings_load_cap', 'manage_options' );
		if ( current_user_can( $capability ) ) {
			if ( isset( $_POST['class'] ) ) {
				$class    = sanitize_text_field( $_POST['class'] );
				$settings = get_option( $class, array() );
				foreach ( $settings as &$setting ) {
					$setting['value'] = stripslashes( $setting['value'] );
				}
				$response = wp_json_encode( $settings );
			} else {
				$response = __( 'Class for addon is not set.', 'uncanny-learndash-toolkit' );
			}
		} else {
			$response = __( 'You must be an admin to save settings.', 'uncanny-learndash-toolkit' );
		}
		echo $response;

		wp_die();

	}

	/**
	 * @param        $key
	 * @param        $class
	 * @param string $default
	 * @param array $class_settings
	 *
	 * @return string
	 */
	public static function get_settings_value( $key, $class, $default = '', $class_settings = array() ) {

		// get module settings key
		$class = str_replace( __NAMESPACE__, '', stripslashes( $class ) );

		// Get all module settings
		$options = get_option( $class, '' );

		// set default settings if placeholder is to be used as default
		if ( '%placeholder%' === $default ) {
			// fallback
			//$default = '';
			foreach ( $class_settings as $setting ) {
				if ( isset( $setting['option_name'] ) && $key === $setting['option_name'] ) {
					if ( isset( $setting['placeholder'] ) ) {
						$default = $setting['placeholder'];
					}
				}
			}
		}

		// Check if setting key has an associated class
		if ( ! empty( $options ) && '' !== $options ) {
			foreach ( $options as $option ) {
				if ( in_array( $key, $option, true ) ) {
					if ( '' !== $default && '' === trim( $option['value'] ) ) {
						return $default;
					}

					return stripslashes( $option['value'] );
				}
			}
		}

		return $default;
	}


	/**
	 * @param string $trace
	 * @param string $trace_name
	 * @param string $file_name
	 */
	public static function trace_logs( $trace = '', $trace_name = '', $file_name = 'logs' ) {
		$timestamp   = date( 'F d, Y H:i:s', current_time( 'timestamp' ) );
		$boundary    = "\n===========================<<<< {$timestamp} >>>>===========================\n";
		$log_type    = "*******************************[[[[[[[[[[ {$trace_name} ]]]]]]]]]]*******************************\n";
		$log_end     = "\n===========================<<<< TRACE END >>>>===========================\n\n";
		$final_trace = print_r( $trace, true );
		$file        = WP_CONTENT_DIR . '/uo-' . $file_name . '.log';
		error_log( $boundary . $log_type . $final_trace . $log_end, 3, $file ); // phpcs:ignore
	}

	/**
	 * Add UTM parameters to a given URL
	 *
	 * @param String $url URL
	 * @param Array $custom_utm_parameters Array with the UTM parameters
	 *
	 * @return String                        URL with the UTM parameters
	 */
	public static function utm_parameters( $url, $medium = '', $content = '' ) {
		// utm_source=plugin-id
		// utm_medium=section-id
		// utm_content=element-id+unique-id

		$default_utm_parameters = array(
			'source' => defined( 'UNCANNY_TOOLKIT_PRO_PATH' ) ? 'uncanny_toolkit_pro' : 'uncanny_toolkit',
		);

		try {
			// Parse the URL
			$url_parts = parse_url( $url );

			// If URL doesn't have a query string.
			if ( isset( $url_parts['query'] ) ) {
				// Avoid 'Undefined index: query'
				parse_str( $url_parts['query'], $params );
			} else {
				$params = array();
			}

			// Add default parameters
			foreach ( $default_utm_parameters as $default_utm_parameter_key => $default_utm_parameter_value ) {
				$params[ 'utm_' . $default_utm_parameter_key ] = $default_utm_parameter_value;
			}

			// Add custom parameters
			if ( ! empty( $medium ) ) {
				$params['utm_medium'] = $medium;
			}

			if ( ! empty( $content ) ) {
				$params['utm_content'] = $content;
			}

			// Encode parameters
			$url_parts['query'] = http_build_query( $params );

			if ( function_exists( 'http_build_url' ) ) {
				// If the user has pecl_http
				$url = http_build_url( $url_parts );
			} else {
				$url_parts['path'] = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '';

				$url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'] . '?' . $url_parts['query'];
			}
		} catch ( Exception $e ) {
		}

		return $url;
	}
}