e cornerstone workout. * @return bool If the payload is valid or not. */ public function cornerstone_is_allowed( $workout ) { return $this->is_allowed( $workout, self::ALLOWED_CORNERSTONE_STEPS ); } /** * Validates the orphaned attribute. * * @param array $workout The orphaned workout. * @return bool If the payload is valid or not. */ public function orphaned_is_allowed( $workout ) { return $this->is_allowed( $workout, self::ALLOWED_ORPHANED_STEPS ); } /** * Validates a workout. * * @param array $workout The workout. * @param array $allowed_steps The allowed steps for this workout. * @return bool If the payload is valid or not. */ public function is_allowed( $workout, $allowed_steps ) { // Only 3 properties are allowed, the below validated finishedSteps property. if ( \count( $workout ) !== 3 ) { return false; } if ( isset( $workout['finishedSteps'] ) && \is_array( $workout['finishedSteps'] ) ) { foreach ( $workout['finishedSteps'] as $step ) { if ( ! \in_array( $step, $allowed_steps, true ) ) { return false; } } return true; } return false; } /** * Saves the Premium workouts data to the database. * * @param mixed|null $result The result of the previous save operations. * @param array $workouts_data The complete workouts data. * * @return mixed|null */ public function save_workouts_data( $result, $workouts_data ) { $premium_workouts_data = []; $premium_workouts_data['cornerstone'] = $workouts_data['cornerstone']; $premium_workouts_data['orphaned'] = $workouts_data['orphaned']; foreach ( $premium_workouts_data as $workout => $data ) { if ( isset( $data['indexablesByStep'] ) && \is_array( $data['indexablesByStep'] ) ) { foreach ( $data['indexablesByStep'] as $step => $indexables ) { if ( $step === 'removed' ) { continue; } $premium_workouts_data[ $workout ]['indexablesByStep'][ $step ] = \wp_list_pluck( $indexables, 'id' ); } } } return $this->options_helper->set( 'workouts', $premium_workouts_data ); } /** * Retrieves the Premium workouts options from the database and adds it to the global array of workouts options. * * @param array $workouts_option The previous content of the workouts options. * * @return array The workouts options updated with the addition of the Premium workouts data. */ public function get_options( $workouts_option ) { $premium_option = $this->options_helper->get( 'workouts' ); if ( ! ( isset( $premium_option['orphaned']['indexablesByStep'] ) && \is_array( $premium_option['orphaned']['indexablesByStep'] ) && isset( $premium_option['cornerstone']['indexablesByStep'] ) && \is_array( $premium_option['cornerstone']['indexablesByStep'] ) ) ) { return \array_merge( $workouts_option, $premium_option ); } // Get all indexable ids from all workouts and all steps. $indexable_ids_in_workouts = [ 0 ]; foreach ( [ 'orphaned', 'cornerstone' ] as $workout ) { foreach ( $premium_option[ $workout ]['indexablesByStep'] as $step => $indexables ) { if ( $step === 'removed' ) { continue; } foreach ( $indexables as $indexable_id ) { $indexable_ids_in_workouts[] = $indexable_id; } } } // Get all indexables corresponding to the indexable ids. $indexables_in_workouts = $this->indexable_repository->find_by_ids( $indexable_ids_in_workouts ); // Extend the workouts option with the indexables data. foreach ( [ 'orphaned', 'cornerstone' ] as $workout ) { // Don't add indexables for steps that are not allowed. $premium_option[ $workout ]['finishedSteps'] = \array_values( \array_intersect( $premium_option[ $workout ]['finishedSteps'], [ 'orphaned' => self::ALLOWED_ORPHANED_STEPS, 'cornerstone' => self::ALLOWED_CORNERSTONE_STEPS, ][ $workout ] ) ); // Don't add indexables that are not published or are no-indexed. foreach ( $premium_option[ $workout ]['indexablesByStep'] as $step => $indexables ) { if ( $step === 'removed' ) { continue; } $premium_option[ $workout ]['indexablesByStep'][ $step ] = \array_values( \array_filter( \array_map( static function( $indexable_id ) use ( $indexables_in_workouts ) { foreach ( $indexables_in_workouts as $updated_indexable ) { if ( \is_array( $indexable_id ) ) { $indexable_id = $indexable_id['id']; } if ( (int) $indexable_id === $updated_indexable->id ) { if ( $updated_indexable->post_status !== 'publish' && $updated_indexable->post_status !== null ) { return false; } if ( $updated_indexable->is_robots_noindex ) { return false; } return $updated_indexable; } } return false; }, $indexables ) ) ); } } return \array_merge( $workouts_option, $premium_option ); } }