'', 'section_title'=>'', 'section_tagline'=>'', 'earlier_count'=>4, 'earlier_columns'=>0, 'color_primary'=>'#1f2a44', 'color_accent'=>'#a9c7e8', 'remove_data_on_uninstall'=>0 ); } public static function defaults() { return array_merge( self::editable_defaults(), array( 'heading_font'=>'serif', 'label_flag'=>'Latest', 'label_read'=>'Read the article', 'label_earlier'=>'Earlier articles', 'label_viewall'=>'View all articles', 'label_subscribe'=>'Subscribe' ) ); } public static function get( $key = null ) { $stored = (array) get_option( self::OPT_SETTINGS, array() ); $s = wp_parse_args( array_intersect_key( $stored, self::editable_defaults() ), self::defaults() ); $s['earlier_count'] = max( 0, min( 4, (int) $s['earlier_count'] ) ); $s['earlier_columns'] = self::columns( $s['earlier_columns'] ); return null === $key ? $s : ( isset( $s[ $key ] ) ? $s[ $key ] : null ); } public static function resolved() { $s = self::get(); $meta = (array) get_option( self::OPT_FEEDMETA, array() ); foreach ( array( 'section_title'=>'title', 'section_tagline'=>'description' ) as $key=>$field ) { if ( '' === $s[ $key ] && ! empty( $meta[ $field ] ) ) { $s[ $key ] = $meta[ $field ]; } } return $s; } public static function base_url() { return preg_replace( '#/feed/?$#', '', untrailingslashit( (string) self::get( 'feed_url' ) ) ); } public static function normalize_feed_url( $url ) { $url = is_string( $url ) ? trim( $url ) : ''; if ( '' === $url ) { return ''; } if ( ! preg_match( '#^https?://#i', $url ) ) { $url = 'https://' . $url; } $url = esc_url_raw( $url ); return preg_match( '#/feed/?$#', $url ) ? $url : untrailingslashit( $url ) . '/feed'; } public static function columns( $value, $fallback = 0 ) { return is_scalar( $value ) && preg_match( '/^[0-4]$/D', (string) $value ) ? (int) $value : $fallback; } public static function byline( array $article ) { return ! empty( $article['author'] ) ? sprintf( /* translators: %s: article author name. */ __( 'By %s', 'stackvik-articles-for-substack' ), $article['author'] ) : ''; } public static function view_all_url() { $base = self::base_url(); return $base ? $base . '/archive' : ''; } public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); // Keep Pro preferences intact when this edition saves its own controls. $out = (array) get_option( self::OPT_SETTINGS, array() ); $d = self::editable_defaults(); $out['feed_url'] = self::normalize_feed_url( isset( $input['feed_url'] ) ? $input['feed_url'] : self::get( 'feed_url' ) ); foreach ( array( 'section_title', 'section_tagline' ) as $key ) { $out[ $key ] = isset( $input[ $key ] ) && is_scalar( $input[ $key ] ) ? sanitize_text_field( $input[ $key ] ) : self::get( $key ); } foreach ( array( 'color_primary', 'color_accent' ) as $key ) { $c = isset( $input[ $key ] ) && is_string( $input[ $key ] ) ? sanitize_hex_color( $input[ $key ] ) : ''; $out[ $key ] = $c ? $c : self::get( $key ); } $out['earlier_count'] = isset( $input['earlier_count'] ) && is_scalar( $input['earlier_count'] ) ? max( 0, min( 4, absint( $input['earlier_count'] ) ) ) : self::get( 'earlier_count' ); $out['earlier_columns'] = self::columns( isset( $input['earlier_columns'] ) ? $input['earlier_columns'] : self::get( 'earlier_columns' ) ); $out['remove_data_on_uninstall'] = empty( $input['remove_data_on_uninstall'] ) ? 0 : 1; if ( $out['feed_url'] !== self::get( 'feed_url' ) ) { self::clear_publication(); } return $out; } public static function clear_publication() { foreach ( array( 'svk_posts','svk_feed_meta','svk_seen_guid','svk_featured','svk_pending','svk_backfill_status' ) as $key ) { delete_option( $key ); } // Remove stale saved listings if this site previously ran Pro with another publication. $ids = get_posts( array( 'post_type'=>'svk_article', 'post_status'=>'any', 'numberposts'=>-1, 'fields'=>'ids', 'meta_query'=>array(array('relation'=>'OR',array('key'=>'_svk_publication','value'=>'main'),array('key'=>'_svk_publication','compare'=>'NOT EXISTS'))) ) ); foreach ( $ids as $id ) { wp_delete_post( $id, true ); } wp_clear_scheduled_hook( 'svk_backfill' ); } public static function entry_from_article( array $a ) { return array_merge( $a, array( 'focal_x'=>50, 'focal_y'=>40, 'alt'=>'', 'has_headline'=>0 ) ); } }