ARING_MODULE !== $module_type ) { $module_mode = empty( $module_mode ) ? $this->module_mode : $module_mode; } if ( ! $with_display_name ) { $metas = array( self::KEY_CONTENT, self::KEY_DESIGN, self::KEY_VISIBILITY ); if ( self::SOCIAL_SHARING_MODULE !== $module_type ) { if ( 'optin' === $module_mode ) { $metas[] = self::KEY_EMAILS; $metas[] = self::KEY_INTEGRATIONS_SETTINGS; } $metas[] = self::KEY_SETTINGS; } if ( self::SOCIAL_SHARING_MODULE === $module_type || self::EMBEDDED_MODULE === $module_type ) { $metas[] = self::KEY_DISPLAY_OPTIONS; } } else { // 0 Content // 1 Emails // 2 Integrations // 3 Appearance // 4 Display Options // 5 Visibility // 6 Behavior $metas = array(); $metas[0] = array( 'name' => self::KEY_CONTENT, 'label' => self::SOCIAL_SHARING_MODULE !== $module_type ? __( 'Content', 'hustle' ) : __( 'Services', 'hustle' ), ); $metas[3] = array( 'name' => self::KEY_DESIGN, 'label' => __( 'Appearance', 'hustle' ), ); $metas[5] = array( 'name' => self::KEY_VISIBILITY, 'label' => __( 'Visibility', 'hustle' ), ); if ( self::SOCIAL_SHARING_MODULE !== $module_type ) { if ( 'optin' === $module_mode ) { $metas[1] = array( 'name' => self::KEY_EMAILS, 'label' => __( 'Emails', 'hustle' ), ); $metas[2] = array( 'name' => self::KEY_INTEGRATIONS_SETTINGS, 'label' => __( 'Integrations', 'hustle' ), ); } $metas[6] = array( 'name' => self::KEY_SETTINGS, 'label' => __( 'Behavior', 'hustle' ), ); } if ( self::SOCIAL_SHARING_MODULE === $module_type || self::EMBEDDED_MODULE === $module_type ) { $metas[4] = array( 'name' => self::KEY_DISPLAY_OPTIONS, 'label' => __( 'Display Options', 'hustle' ), ); } // Order and return without the keys. ksort( $metas ); $metas = array_values( $metas ); } return $metas; } /** * Retrieve the module's metas as an array. * * @since 4.0.1 * @return array */ public function get_module_metas_as_array() { $metas_array = array(); $module_metas = $this->get_module_meta_names(); foreach ( $module_metas as $meta ) { $method = 'get_' . $meta; $value = method_exists( $this, $method ) ? $this->{$method}()->to_array() : array(); $metas_array[ $meta ] = $value; } return $metas_array; } /** * Return whether the provided role can edit at least one module. * * @since 4.1.0 * @param string $role_slug The slug of the role to be checked. * @return boolean */ public static function can_role_edit_one_module( $role_slug ) { global $wpdb; $table = Hustle_Db::modules_meta_table(); $query = $wpdb->prepare( "SELECT module_id FROM `{$table}` WHERE `meta_key`='edit_roles' AND meta_value LIKE %s LIMIT 1", '%"' . $role_slug . '"%' );// phpcs:ignore return $wpdb->get_var( $query );// phpcs:ignore } /** * Returns format for optin table * * @since 1.0.0 * * @return array */ private function get_format() { return array( 'module_name' => '%s', 'module_type' => '%s', 'active' => '%d', 'module_mode' => '%s', ); } /** * Special save used in migration. * It keeps the passed module id when saving a new module. * It's useful when adding old modules in new tables in MU. * * @since 4.0.0 * @return false|int */ public function save_from_migration() { $module_data = get_object_vars( $this ); $table = Hustle_Db::modules_table(); $data = $this->sanitize_model_data( $module_data ); $format = $this->get_format(); $format = array_values( $format ); /** * Add ID */ $data['module_id'] = $this->module_id; $this->id = $this->module_id; $format[] = '%d'; $this->wpdb->insert( $table, $data, $format ); /** * Action Hustle after migration module * * @since 4.0.0 * * @param string $module_type module type * @param array $data module data * @param int $id module id */ do_action( 'hustle_after_migrate_module', $this->module_type, $module_data, $this->id ); // Clear cache as well. $this->clean_module_cache( 'data' ); return $this->id; } /** * Check Visibility conditions. Returns true if conditions are matched * * @return boolean */ public function is_condition_allow() { $allow = true; $sub_type = ! empty( $this->sub_type ) ? $this->sub_type : null; if ( ! $this->get_visibility()->is_allowed_to_display( $this->module_type, $sub_type ) ) { $allow = false; } return $allow; } /** * Get featured image alt text * * @return string */ public function get_feature_image_alt() { if ( isset( $this->content->feature_image_alt ) ) { return $this->content->feature_image_alt; } $alt = ! empty( $this->content->feature_image ) ? $this->update_feature_image_alt( $this->content->feature_image ) : ''; return $alt; } /** * Update feature image alt text * * @param string $image Image URL. * @param boolean $force_save Save module data. * @return type */ public function update_feature_image_alt( $image, $force_save = true ) { $thumb_id = attachment_url_to_postid( $image ); $text = $thumb_id ? get_post_meta( $thumb_id, '_wp_attachment_image_alt', true ) : ''; if ( $force_save && $this->id ) { $content = $this->get_content()->to_array(); $content['feature_image_alt'] = $text; $this->update_meta( self::KEY_CONTENT, $content ); } return $text; } /** * Opt_In_Data constructor. * * @param int $module_id Module ID. * @return \WP_Error|Hustle_Module_Model */ public static function new_instance( $module_id = null ) { $module = new static(); // If no ID is provided, return an empty model. // This is useful when creating a new module. if ( is_null( $module_id ) ) { return $module; } if ( ! is_numeric( $module_id ) || $module_id < 1 ) { return new WP_Error( 'invalid_module_id', __( 'The provided module ID is invalid.', 'hustle' ) ); } $result = $module->init( $module_id ); if ( is_wp_error( $result ) ) { return $result; } return $module; } }