mapping = array( 'custom-field' => 'custom-field', 'fixed-text' => 'custom-field', 'accept-user-rating' => 'custom-field', 'create-field' => 'custom-field', 'custom-text' => 'custom-field', 'specific-field' => 'specific-field', ); } /** * Init Metabox user rile dependancies. */ public function aiosrs_custom_allowed_user_role() { $allowed_user = apply_filters( 'wp_schema_pro_role', array( 'administrator' ) ); update_option( 'custom_user_role', $allowed_user ); } /** * Rest star rating. */ public function aiosrs_reset_post_rating_callback() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error(); } check_ajax_referer( 'schema-pro-reset-rating', 'nonce' ); $response = array( 'success' => false, ); if ( isset( $_POST['post_id'] ) ) { $post_id = sanitize_text_field( $_POST['post_id'] ); $schema_id = isset( $_POST['schema_id'] ) ? sanitize_text_field( $_POST['schema_id'] ) : ''; delete_post_meta( $post_id, 'bsf-schema-pro-reviews-' . $schema_id ); delete_post_meta( $post_id, 'bsf-schema-pro-review-counts-' . $schema_id ); delete_post_meta( $post_id, 'bsf-schema-pro-rating-' . $schema_id ); $response['success'] = true; $response['rating-avg'] = _x( '0/5', 'rating out of', 'wp-schema-pro' ); $response['review-count'] = __( '(0 Reviews)', 'wp-schema-pro' ); } wp_send_json( $response ); } /** * Custom Fields Shortcode. * * @param array $atts Shortcode attributes. * @return html */ public function shortcode_callback( $atts ) { $args = shortcode_atts( array( 'post_id' => '', 'post_type' => 'post', 'field_key' => '', 'default' => '', ), $atts ); $post_id = empty( $args['post_id'] ) ? get_the_ID() : (int) $args['post_id']; $post = get_post( $post_id ); if ( ! current_user_can( 'edit_post', $post_id ) || ( ! current_user_can( 'read_private_posts' ) && ( 'private' === $post->post_status ) ) ) { return ''; // Insufficient permissions or invalid post. } $allowed_post_types = array( 'post', 'page', 'comment', 'term' ); if ( ! in_array( $args['post_type'], $allowed_post_types ) ) { return 'Invalid post type'; } $output = get_metadata( $args['post_type'], $post_id, $args['field_key'], true ); if ( empty( $output ) || is_array( $output ) ) { $output = $args['default']; } return $output; } /** * Meta Boxes Style * * @return void */ public function meta_boxes_style() { if ( ! empty( self::$meta_boxes ) ) { $ids = array(); foreach ( self::$meta_boxes as $key => $meta_box ) { $ids[] = '#aiosrs_pro_custom_meta_box_' . $key . ' .inside'; } echo ''; } } /** * Init Metabox */ public function init_metabox() { $screen = get_current_screen(); $current_post_type = $screen->post_type; if ( 'aiosrs-schema' === $current_post_type || 'acf-field-group' === $current_post_type ) { return; } $allowed_user_roles = array(); $user = wp_get_current_user(); if ( is_array( $allowed_user_roles ) ) { $allowed_user_roles = get_option( 'custom_user_role' ); } if ( array_intersect( $allowed_user_roles, (array) $user->roles ) ) { add_action( 'add_meta_boxes', array( $this, 'setup_meta_box' ) ); add_action( 'save_post', array( $this, 'save_meta_box' ) ); } } /** * Initializing static variable. * * @param int|boolean $current_post_id Post Id. * @return void */ public function init_static_fields( $current_post_id = false ) { $option = array( 'location' => 'bsf-aiosrs-schema-location', 'exclusion' => 'bsf-aiosrs-schema-exclusion', ); $schema_post_result = BSF_Target_Rule_Fields::get_instance()->get_posts_by_conditions( 'aiosrs-schema', $option, $current_post_id ); if ( is_array( $schema_post_result ) && ! empty( $schema_post_result ) ) { $current_post_id = get_the_id(); foreach ( $schema_post_result as $post_id => $post_data ) { $schema_type = get_post_meta( $post_id, 'bsf-aiosrs-schema-type', true ); $schema_meta = get_post_meta( $post_id, 'bsf-aiosrs-' . $schema_type, true ); if ( empty( $current_post_id ) || empty( $schema_type ) || empty( $schema_meta ) ) { continue; } $schema_meta_fields = BSF_AIOSRS_Pro_Schema::$schema_meta_fields[ 'bsf-aiosrs-' . $schema_type ]['subkeys']; $review_schema_type = BSF_AIOSRS_Pro_Schema::$schema_meta_fields['bsf-aiosrs-review']['subkeys']['schema-type']['choices']; $item_schema_type = isset( $schema_meta['schema-type'] ) ? $schema_meta['schema-type'] : ''; foreach ( $review_schema_type as $review_type_key => $review_type ) { if ( ! ( empty( $item_schema_type ) ) && ( $item_schema_type === $review_type_key ) ) { $temp = BSF_AIOSRS_Pro_Schema::$schema_item_types[ $item_schema_type ]; if ( isset( $temp['subkeys'] ) ) { $schema_meta_item_fields = $temp['subkeys']; } } } $custom_fields = array(); foreach ( $schema_meta as $schema_key => $schema_value ) { if ( isset( $schema_meta_fields[ $schema_key ] ) ) { $schema_field_value = $schema_meta_fields[ $schema_key ]; } else { if ( isset( $schema_meta['schema-type'] ) ) { $item_schema_key = str_replace( $schema_meta['schema-type'] . '-', '', $schema_key ); } $item_schema_key = isset( $item_schema_key ) ? $item_schema_key : ''; $schema_field_value = isset( $schema_meta_item_fields[ $item_schema_key ] ) ? $schema_meta_item_fields[ $item_schema_key ] : null; } if ( 'applicant-location' === $schema_key ) { $schema_field_value = array( 'label' => esc_html__( 'Applicant Location', 'wp-schema-pro' ), 'type' => 'text', 'default' => 'none', 'required' => false, 'description' => esc_html__( 'The geographic location(s) in which employees may be located to be eligible for the Remote job.', 'wp-schema-pro' ), ); } $repeater_values = array(); if ( $schema_field_value ) { if ( 'repeater' === $schema_field_value['type'] ) { $repeater_values = get_post_meta( $current_post_id, $schema_type . '-' . $post_id . '-' . $schema_key, true ); // Added backward applicant location field dependancy. if ( 'remote-location' === $schema_key ) { $applicant_location_string = get_post_meta( $current_post_id, 'job-posting-' . $post_id . '-applicant-location', true ); $dep_count = get_option( 'wp_backward_field' . $current_post_id . '' . $post_id ); $dep_count = ! empty( $dep_count ) ? $dep_count : ''; if ( $applicant_location_string !== $dep_count && '' === $dep_count ) { $deprecated_application_location = array( array( 'applicant-location' => ! empty( $applicant_location_string ) ? $applicant_location_string : '', 'applicant-location-fieldtype' => 'custom-field', 'applicant-location-connected' => 'none', 'applicant-location-custom' => ! empty( $applicant_location_string ) ? $applicant_location_string : '', 'applicant-location-specific' => 'none', ), ); if ( isset( $deprecated_application_location ) && ! empty( $deprecated_application_location ) ) { if ( ! empty( $repeater_values ) ) { $repeater_values = array_merge( $deprecated_application_location, $repeater_values ); update_option( 'wp_backward_field' . $current_post_id . '' . $post_id, $applicant_location_string ); } else { $repeater_values = $deprecated_application_location; update_option( 'wp_backward_field' . $current_post_id . '' . $post_id, $applicant_location_string ); } } update_post_meta( $current_post_id, $schema_type . '-' . $post_id . '-' . $schema_key, $repeater_values ); } } if ( ! is_array( $repeater_values ) || empty( $repeater_values ) ) { $repeater_values = $schema_meta[ $schema_key ]; } $repeter_fields = $schema_meta_fields[ $schema_key ]['fields']; $tmp_fields = array(); foreach ( $repeater_values as $index => $repeater_value ) { foreach ( $schema_field_value['fields'] as $field_key => $field ) { $field_val = isset( $schema_meta[ $schema_key ][ $index ][ $field_key ] ) ? $schema_meta[ $schema_key ][ $index ][ $field_key ] : ''; if ( 'create-field' === $field_val ) { $selected_field = 'custom-field'; $selected_value = ''; } elseif ( isset( $schema_meta[ $schema_key ][ $index ][ $field_key . '-' . $field_val ] ) ) { $selected_field = isset( $this->mapping[ $field_val ] ) ? $this->mapping[ $field_val ] : $field_val; $selected_value = $schema_meta[ $schema_key ][ $index ][ $field_key . '-' . $field_val ]; } elseif ( isset( $this->mapping[ $field_val ] ) ) { $selected_field = $this->mapping[ $field_val ]; $selected_value = ''; } else { $selected_field = 'global-field'; $selected_value = $field_val; } $tmp_fields[ $index ][] = array( 'default' => isset( $repeater_value[ $field_key ] ) ? $repeater_value[ $field_key ] : '', 'name' => $schema_type . '-' . $post_id . '-' . $schema_key . '[' . $index . '][' . $field_key . ']', 'fieldtype' => $schema_type . '-' . $post_id . '-' . $schema_key . '[' . $index . '][' . $field_key . '-fieldtype]', 'type' => $field['type'], 'label' => $field['label'], 'required' => isset( $field['required'] ) ? $field['required'] : false, 'min' => isset( $repeter_fields[ $field_key ]['attrs']['min'] ) ? $repeter_fields[ $field_key ]['attrs']['min'] : '', 'step' => isset( $repeter_fields[ $field_key ]['attrs']['step'] ) ? $repeter_fields[ $field_key ]['attrs']['step'] : '', 'description' => isset( $repeter_fields[ $field_key ]['description'] ) ? $repeter_fields[ $field_key ]['description'] : '', 'dropdown-content' => isset( $repeter_fields[ $field_key ]['dropdown-type'] ) ? $repeter_fields[ $field_key ]['dropdown-type'] : '', 'global_fieldtype' => $selected_field, 'global_default' => $selected_value, 'class' => isset( $field['class'] ) ? $field['class'] : '', 'subkey_data' => $field, ); } } $custom_fields[] = array( 'default' => isset( $schema_meta[ $schema_key . '-custom-meta-default' ] ) ? $schema_meta[ $schema_key . '-custom-meta-default' ] : '', 'name' => $schema_type . '-' . $post_id . '-' . $schema_key, 'type' => $schema_field_value['type'], 'label' => $schema_field_value['label'], 'min' => isset( $schema_field_value['attrs']['min'] ) ? $schema_field_value['attrs']['min'] : '', 'step' => isset( $schema_field_value['attrs']['step'] ) ? $schema_field_value['attrs']['step'] : '', 'required' => isset( $schema_field_value['required'] ) ? $schema_field_value['required'] : false, 'dropdown-content' => isset( $schema_field_value['dropdown-type'] ) ? $schema_field_value['dropdown-type'] : '', 'user-rating' => 'accept-user-rating' === $schema_value, 'description' => isset( $schema_field_value['description'] ) ? $schema_field_value['description'] : '', 'fields' => $tmp_fields, 'global_fieldtype' => '', 'global_default' => '', ); } elseif ( 'repeater-target' === $schema_field_value['type'] ) { $repeater_values = get_post_meta( $current_post_id, $schema_type . '-' . $post_id . '-' . $schema_key, true ); if ( ! is_array( $repeater_values ) || empty( $repeater_values ) ) { $repeater_values = array( array_fill_keys( array_keys( $schema_field_value['fields'] ), '' ) ); } $tmp_fields = array(); foreach ( $repeater_values as $key => $repeater_value ) { foreach ( $schema_field_value['fields'] as $field_key => $field ) { $tmp_fields[ $key ][] = array( 'default' => $repeater_value[ $field_key ], 'name' => $schema_type . '-' . $post_id . '-' . $schema_key . '[' . $key . '][' . $field_key . ']', 'type' => $field['type'], 'label' => $field['label'], 'required' => isset( $field['required'] ) ? $field['required'] : false, 'description' => isset( $field['description'] ) ? $field['description'] : '', ); } } $custom_fields[] = array( 'default' => isset( $schema_meta[ $schema_key . '-custom-meta-default' ] ) ? $schema_meta[ $schema_key . '-custom-meta-default' ] : '', 'name' => $schema_type . '-' . $post_id . '-' . $schema_key, 'type' => $schema_field_value['type'], 'label' => $schema_field_value['label'], 'min' => isset( $schema_field_value['attrs']['min'] ) ? $schema_field_value['attrs']['min'] : '', 'step' => isset( $schema_field_value['attrs']['step'] ) ? $schema_field_value['attrs']['step'] : '', 'required' => isset( $schema_field_value['required'] ) ? $schema_field_value['required'] : false, 'dropdown-content' => isset( $schema_field_value['dropdown-type'] ) ? $schema_field_value['dropdown-type'] : '', 'user-rating' => 'accept-user-rating' === $schema_value, 'description' => isset( $schema_field_value['description'] ) ? $schema_field_value['description'] : '', 'fields' => $tmp_fields, 'global_fieldtype' => '', 'global_default' => '', ); } else { if ( ! isset( $schema_meta['bsf-aiosrs-software-application-rating'] ) ) { $schema_meta['bsf-aiosrs-software-application-rating'] = ''; } if ( ! isset( $schema_meta['bsf-aiosrs-product-rating'] ) ) { $schema_meta['bsf-aiosrs-product-rating'] = ''; } // Skip review count in case of Accept user rating. if ( ( 'bsf-aiosrs-product-review-count' === $schema_key || 'bsf-aiosrs-software-application-review-count' === $schema_key || 'review-count' === $schema_key ) && ( 'accept-user-rating' === $schema_meta['rating'] || 'accept-user-rating' === $schema_meta['bsf-aiosrs-software-application-rating'] || 'accept-user-rating' === $schema_meta['bsf-aiosrs-product-rating'] ) ) { continue; } if ( 'create-field' === $schema_meta[ $schema_key ] ) { $selected_field = 'custom-field'; $selected_value = ''; } elseif ( isset( $schema_meta[ $schema_key . '-' . $schema_meta[ $schema_key ] ] ) ) { $selected_field = isset( $this->mapping[ $schema_meta[ $schema_key ] ] ) ? $this->mapping[ $schema_meta[ $schema_key ] ] : $schema_meta[ $schema_key ]; $selected_value = $schema_meta[ $schema_key . '-' . $schema_meta[ $schema_key ] ]; } elseif ( isset( $this->mapping[ $schema_meta[ $schema_key ] ] ) ) { $selected_field = $this->mapping[ $schema_meta[ $schema_key ] ]; $selected_value = ''; } else { $selected_field = 'global-field'; $selected_value = $schema_meta[ $schema_key ]; } $custom_fields[] = array( 'default' => isset( $schema_meta[ $schema_key . '-custom-meta-default' ] ) ? $schema_meta[ $schema_key . '-custom-meta-default' ] : '', 'name' => $schema_type . '-' . $post_id . '-' . $schema_key, 'type' => $schema_field_value['type'], 'label' => $schema_field_value['label'], 'min' => isset( $schema_field_value['attrs']['min'] ) ? $schema_field_value['attrs']['min'] : '', 'step' => isset( $schema_field_value['attrs']['step'] ) ? $schema_field_value['attrs']['step'] : '', 'required' => isset( $schema_field_value['required'] ) ? $schema_field_value['required'] : false, 'dropdown-content' => isset( $schema_field_value['dropdown-type'] ) ? $schema_field_value['dropdown-type'] : '', 'user-rating' => 'accept-user-rating' === $schema_value, 'description' => isset( $schema_field_value['description'] ) ? $schema_field_value['description'] : '', 'subkey' => $schema_key, 'subkey_data' => $schema_field_value, 'global_fieldtype' => $selected_field, 'global_default' => $selected_value, ); } } } if ( ! empty( $custom_fields ) ) { $schema_enabled = self::enable_schema_post_option(); if ( $schema_enabled ) { array_unshift( $custom_fields, array( 'default' => 'disabled', 'name' => $schema_type . '-' . $post_id . '-enabled-schema', ) ); } self::$meta_boxes[ $post_id ] = array( 'ID' => $post_id, 'post_title' => get_the_title( $post_id ), 'schema_type' => $schema_type, 'fields' => $custom_fields, ); self::$meta_options[] = $custom_fields; } } self::$meta_options = array_reduce(self::$meta_options, function ($carry, $item) { if (is_array($item)) { return array_merge($carry, $item); } return $carry; }, []); } } /** * Setup Metabox */ public function setup_meta_box() { $brand_settings = BSF_AIOSRS_Pro_Helper::$settings['wp-schema-pro-branding-settings']; $this->init_static_fields(); if ( ! empty( self::$meta_boxes ) ) { if ( '' !== $brand_settings['sp_plugin_name'] ) { $title = __( $brand_settings['sp_plugin_name'], 'wp-schema-pro' ); //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText } else { $title = __( 'Schema Pro', 'wp-schema-pro' ); } if ( count( self::$meta_boxes ) === 1 ) { $key = key( self::$meta_boxes ); $title .= ' - ' . self::$meta_boxes[ $key ]['post_title']; } $current_post_type = get_post_type(); add_meta_box( 'aiosrs-pro-custom-fields', $title, array( $this, 'custom_field_markup' ), $current_post_type ); } } /** * Function to enable option. * * @since 1.1.1 * @return boolean */ public static function enable_schema_post_option() { return apply_filters( 'wp_schema_pro_default_markup', false ); } /** * Custom Fields meta Markup. * * @param object $post Post Object. * @return void */ public function custom_field_markup( $post ) { wp_nonce_field( basename( __FILE__ ), 'aiosrs-pro-custom-meta' ); $stored = get_post_meta( $post->ID ); $tmp_post = get_post( $post->ID, ARRAY_A ); $stored['post'] = $tmp_post; $first_tab = true; $schema_enabled = self::enable_schema_post_option(); do_action( 'aiosrs_pro_custom_fields_markup_before', $post, $stored ); if ( count( self::$meta_boxes ) > 1 ) { ?>