wordpress.php
149 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WordPress core integrations file |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package SureTrigger |
| 7 | */ |
| 8 | |
| 9 | namespace SureTriggers\Integrations\WordPress; |
| 10 | |
| 11 | use SureTriggers\Controllers\IntegrationsController; |
| 12 | use SureTriggers\Integrations\Integrations; |
| 13 | use SureTriggers\Traits\SingletonLoader; |
| 14 | |
| 15 | /** |
| 16 | * Class WordPress |
| 17 | * |
| 18 | * @package SureTriggers\Integrations\Wordpress |
| 19 | */ |
| 20 | class WordPress extends Integrations { |
| 21 | |
| 22 | |
| 23 | use SingletonLoader; |
| 24 | |
| 25 | /** |
| 26 | * ID |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $id = 'WordPress'; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Get user context data. |
| 35 | * |
| 36 | * @param int $id ID. |
| 37 | * |
| 38 | * @return array |
| 39 | */ |
| 40 | public static function get_user_context( $id ) { |
| 41 | |
| 42 | $user = get_userdata( $id ); |
| 43 | $context = []; |
| 44 | if ( ! $user ) { |
| 45 | return $context; |
| 46 | } |
| 47 | $context['wp_user_id'] = $user->ID; |
| 48 | $context['user_login'] = $user->user_login; |
| 49 | $context['display_name'] = $user->display_name; |
| 50 | $context['user_firstname'] = $user->user_firstname; |
| 51 | $context['user_lastname'] = $user->user_lastname; |
| 52 | $context['user_email'] = $user->user_email; |
| 53 | $context['user_registered'] = $user->user_registered; |
| 54 | $context['user_role'] = $user->roles; |
| 55 | return $context; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get sample user context data. |
| 60 | * |
| 61 | * @return string[] |
| 62 | */ |
| 63 | public static function get_sample_user_context() { |
| 64 | return [ |
| 65 | 'wp_user_id' => '1', |
| 66 | 'user_login' => 'john_doe', |
| 67 | 'display_name' => 'John Doe', |
| 68 | 'user_firstname' => 'John', |
| 69 | 'user_lastname' => 'Doe', |
| 70 | 'user_email' => 'johnd@gmail.com', |
| 71 | 'user_registered' => '2024-06-18 09:47:58', |
| 72 | 'user_role' => 'active', |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get post context data. |
| 78 | * |
| 79 | * @param int $id ID. |
| 80 | * |
| 81 | * @return array |
| 82 | */ |
| 83 | public static function get_post_context( $id ) { |
| 84 | $post_data = (array) get_post( $id ); |
| 85 | |
| 86 | // Add permalink to post context. |
| 87 | if ( ! empty( $post_data ) && isset( $post_data['ID'] ) ) { |
| 88 | $post_data['permalink'] = get_permalink( $post_data['ID'] ); |
| 89 | } |
| 90 | |
| 91 | return $post_data; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets the post meta |
| 96 | * |
| 97 | * @param int $id ID. |
| 98 | * |
| 99 | * @return mixed |
| 100 | */ |
| 101 | public static function get_post_meta( $id ) { |
| 102 | return get_post_meta( $id ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Validating the Email |
| 107 | * |
| 108 | * @param string $email email. |
| 109 | * @return object{valid: bool, multiple: bool} |
| 110 | */ |
| 111 | public static function validate_email( $email ) { |
| 112 | $result = [ |
| 113 | 'valid' => true, |
| 114 | 'multiple' => false, |
| 115 | ]; |
| 116 | |
| 117 | if ( str_contains( $email, ',' ) ) { |
| 118 | $email_list = explode( ',', $email ); |
| 119 | |
| 120 | foreach ( $email_list as $single_email ) { |
| 121 | if ( ! is_email( trim( $single_email ) ) ) { |
| 122 | $result['valid'] = false; |
| 123 | $result['multiple'] = true; |
| 124 | |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } else { |
| 129 | if ( ! is_email( trim( $email ) ) ) { |
| 130 | $result['valid'] = false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return (object) $result; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Is Plugin depended plugin is installed or not. |
| 139 | * |
| 140 | * @return bool |
| 141 | */ |
| 142 | public function is_plugin_installed() { |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | |
| 148 | IntegrationsController::register( WordPress::class ); |
| 149 |