slug = $args['slug'] ?? ''; $this->label = $args['label'] ?? ''; $this->description = $args['description'] ?? ''; $this->options = $args['options'] ?? []; $this->current_base = \SureCart::settings()->permalinks()->getBase( "{$this->slug}_page" ); $this->sample_preview_text = $args['sample_preview_text'] ?? 'sample-product'; } /** * Boostrap settings. * * @return void */ public function bootstrap() { add_action( 'admin_init', [ $this, 'settingsInit' ] ); } /** * Add translations. * * @return void */ public function addTranslations() { $translations['surecart.settings.permalinks.product'] = [ 'slug' => 'product', 'label' => __( 'SureCart Product Permalinks', 'surecart' ), /* translators: %s: Home URL */ 'description' => sprintf( __( 'If you like, you may enter custom structures for your product page URLs here. For example, using products would make your product buy links like %sproducts/sample-product/.', 'surecart' ), esc_url( home_url( '/' ) ) ), 'options' => [ [ 'value' => 'products', 'label' => __( 'Default', 'surecart' ), ], [ 'value' => 'shop', 'label' => __( 'Shop', 'surecart' ), ], ], ]; $translations['surecart.settings.permalinks.buy'] = [ 'slug' => 'buy', 'label' => __( 'SureCart Instant Checkout Permalinks', 'surecart' ), /* translators: %s: Home URL */ 'description' => sprintf( __( 'If you like, you may enter custom structures for your instant checkout URLs here. For example, using buy would make your product buy links like %sbuy/sample-product/.', 'surecart' ), esc_url( home_url( '/' ) ) ), 'options' => [ [ 'value' => 'buy', 'label' => __( 'Default', 'surecart' ), ], [ 'value' => 'purchase', 'label' => __( 'Purchase', 'surecart' ), ], [ 'value' => 'shop/%sc_collection%', 'label' => __( 'Shop base with collection', 'surecart' ), ], ], ]; $translations['surecart.settings.permalinks.buy'] = [ 'slug' => 'buy', 'label' => __( 'SureCart Instant Checkout Permalinks', 'surecart' ), /* translators: %s: Home URL */ 'description' => sprintf( __( 'If you like, you may enter custom structures for your instant checkout URLs here. For example, using buy would make your product buy links like %sbuy/sample-product/.', 'surecart' ), esc_url( home_url( '/' ) ) ), 'options' => [ [ 'value' => 'buy', 'label' => __( 'Default', 'surecart' ), ], [ 'value' => 'purchase', 'label' => __( 'Purchase', 'surecart' ), ], ], ]; $translations['surecart.settings.permalinks.collection'] = [ 'slug' => 'collection', 'label' => __( 'SureCart Product Collection Permalinks', 'surecart' ), /* translators: %s: Home URL */ 'description' => sprintf( __( 'If you like, you may enter custom structures for your product page URLs here. For example, using collections would make your product collection links like %scollections/sample-collection/.', 'surecart' ), esc_url( home_url( '/' ) ) ), 'options' => [ [ 'value' => 'collections', 'label' => __( 'Default', 'surecart' ), ], [ 'value' => 'product-collections', 'label' => __( 'Product Collections', 'surecart' ), ], ], 'sample_preview_text' => 'sample-collection', ]; $translations['surecart.settings.permalinks.upsell'] = [ 'slug' => 'upsell', 'label' => __( 'SureCart Upsell Permalinks', 'surecart' ), /* translators: %s: Home URL */ 'description' => sprintf( __( 'If you like, you may enter custom structures for your upsell URLs here. For example, using offers would make your upsell\'s links like %soffers/upsell-id/.', 'surecart' ), esc_url( home_url( '/' ) ) ), 'options' => [ [ 'value' => 'offer', 'label' => __( 'Default', 'surecart' ), ], [ 'value' => 'special-offer', 'label' => __( 'Special Offer', 'surecart' ), ], ], ]; } /** * Initialize settings. */ public function settingsInit() { $this->addTranslations(); $this->addSettingsSection(); $this->maybeSaveSettings(); } /** * Add sections to permalinks page. */ public function addSettingsSection() { add_settings_section( "surecart-$this->slug-permalink", $this->label, [ $this, 'settings' ], 'permalink' ); } /** * Display the settings. */ public function settings() { echo wp_kses_post( wpautop( $this->description ) ); $values = array_values( array_map( function ( $permalink ) { return trailingslashit( $permalink['value'] ); }, $this->options ) ); ?> options as $permalink ) : ?> slug ) . '_permalink_structure'; $permalink_key = 'sc_' . esc_attr( $this->slug ) . '_permalink'; // we must have our permalink post data and nonce. if ( ! isset( $_POST[ $structure_key ], $_POST[ $permalink_key ] ) || ! wp_verify_nonce( wp_unslash( $_POST['surecart-permalinks-nonce'] ), 'surecart-permalinks' ) ) { // WPCS: input var ok, sanitization ok. return; } // get the buy base. $page = isset( $_POST[ $permalink_key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $permalink_key ] ) ) : ''; $page_struct = isset( $_POST[ $structure_key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $structure_key ] ) ) : ''; if ( 'custom' === $page ) { $page = ! empty( $_POST[ $structure_key ] ) ? preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( wp_unslash( $_POST[ $structure_key ] ) ) ) ) : $this->options[0]['value']; // WPCS: input var ok, sanitization ok. } elseif ( empty( $page ) ) { $page = $this->options[0]['value']; } \SureCart::settings()->permalinks()->updatePermalinkSettings( $this->slug . '_page', $page ); } }