Skip to content

Commit 7307910

Browse files
Add openrouter API support
1 parent 34b9dab commit 7307910

File tree

7 files changed

+126
-13
lines changed

7 files changed

+126
-13
lines changed

includes/admin/feedzy-rss-feeds-actions.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,17 @@ private function chat_gpt_rewrite() {
472472
return $this->current_job->data->ChatGPT;
473473
}
474474

475-
$content = call_user_func( array( $this, $this->current_job->tag ) );
476-
$content = wp_strip_all_tags( $content );
477-
$content = substr( $content, 0, apply_filters( 'feedzy_chat_gpt_content_limit', 3000 ) );
478-
$prompt_content = $this->current_job->data->ChatGPT;
475+
$content = call_user_func( array( $this, $this->current_job->tag ) );
476+
$content = wp_strip_all_tags( $content );
477+
$content = substr( $content, 0, apply_filters( 'feedzy_chat_gpt_content_limit', 3000 ) );
478+
$prompt_content = $this->current_job->data->ChatGPT;
479+
$ai_provider = 'openai';
480+
if ( isset( $this->current_job->data ) && isset( $this->current_job->data->aiProvider ) ) {
481+
$ai_provider = $this->current_job->data->aiProvider;
482+
}
479483
$content = str_replace( array( '{content}' ), array( $content ), $prompt_content );
480484
$openai = new \Feedzy_Rss_Feeds_Pro_Openai();
481-
$rewrite_content = $openai->call_api( $this->settings, $content, '', array() );
485+
$rewrite_content = $openai->call_api( $this->settings, $content, '', array( 'ai_provider' => $ai_provider ) );
482486
// Replace prompt content string for specific cases.
483487
$rewrite_content = str_replace( explode( '{content}', $prompt_content ), '', trim( $rewrite_content, '"' ) );
484488
return $rewrite_content;

includes/admin/feedzy-rss-feeds-admin.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,7 @@ public function api_license_status() {
16391639
'wordaiStatus' => false,
16401640
'openaiStatus' => false,
16411641
'amazonStatus' => false,
1642+
'openRouterStatus' => false,
16421643
);
16431644

16441645
if ( ! feedzy_is_pro() ) {
@@ -1660,6 +1661,12 @@ public function api_license_status() {
16601661
}
16611662
}
16621663

1664+
if ( isset( $pro_options['openrouter_licence'] ) && 'yes' === $pro_options['openrouter_licence'] ) {
1665+
if ( apply_filters( 'feedzy_is_license_of_type', false, 'business' ) || apply_filters( 'feedzy_is_license_of_type', false, 'agency' ) ) {
1666+
$data['openRouterStatus'] = true;
1667+
}
1668+
}
1669+
16631670
if ( ! empty( $pro_options['amazon_access_key'] ) && ! empty( $pro_options['amazon_secret_key'] ) ) {
16641671
$data['amazonStatus'] = true;
16651672
}

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2527,10 +2527,12 @@ public function settings_tabs( $tabs ) {
25272527
*/
25282528
public function integration_tabs( $tabs ) {
25292529
if ( $this->feedzy_is_business() || $this->feedzy_is_agency() ) {
2530-
$tabs['openai'] = __( 'OpenAI', 'feedzy-rss-feeds' );
2530+
$tabs['openai'] = __( 'OpenAI', 'feedzy-rss-feeds' );
2531+
$tabs['openrouter'] = __( 'OpenRouter', 'feedzy-rss-feeds' );
25312532
}
25322533
if ( ! feedzy_is_pro() ) {
25332534
$tabs['openai'] = sprintf( '%s <span class="pro-label">PRO</span>', __( 'OpenAI', 'feedzy-rss-feeds' ) );
2535+
$tabs['openrouter'] = sprintf( '%s <span class="pro-label">PRO</span>', __( 'OpenRouter', 'feedzy-rss-feeds' ) );
25342536
$tabs['spinnerchief'] = sprintf( '%s <span class="pro-label">PRO</span>', __( 'SpinnerChief', 'feedzy-rss-feeds' ) );
25352537
$tabs['amazon-product-advertising'] = sprintf( '%s <span class="pro-label">PRO</span>', __( 'Amazon Product Advertising', 'feedzy-rss-feeds' ) );
25362538
$tabs['wordai'] = sprintf( '%s <span class="pro-label">PRO</span>', __( 'WordAi', 'feedzy-rss-feeds' ) );
@@ -2576,6 +2578,7 @@ private function render_view( $name ) {
25762578
case 'spinnerchief':
25772579
case 'amazon-product-advertising':
25782580
case 'openai':
2581+
case 'openrouter':
25792582
if ( ! feedzy_is_pro() ) {
25802583
$file = FEEDZY_ABSPATH . '/includes/views/' . $name . '-view.php';
25812584
} else {

includes/layouts/integration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ class="<?php echo $_tab === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php e
7878
if ( $fields ) {
7979

8080
foreach ( $fields as $field ) {
81-
echo wp_kses( $field['content'], apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
81+
if ( ! empty( $field['content'] ) ) {
82+
echo wp_kses( $field['content'], apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
83+
}
8284
if ( isset( $field['ajax'] ) && $field['ajax'] ) {
8385
$show_button = false;
8486
}

includes/views/openrouter-view.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<div class="fz-form-wrap">
2+
<div class="form-block">
3+
<div class="upgrade-alert mb-24">
4+
<?php
5+
$upgrade_url = tsdk_translate_link( tsdk_utmify( FEEDZY_UPSELL_LINK, 'openrouter' ), 'query' );
6+
7+
$content = __( 'You are using Feedzy Lite.', 'feedzy-rss-feeds' ) . ' ';
8+
// translators: %1$s: opening anchor tag, %2$s: closing anchor tag
9+
$content .= wp_sprintf( __( 'Unlock more powerful features, by %1$s upgrading to Feedzy Pro %2$s', 'feedzy-rss-feeds' ), '<a href="' . esc_url( $upgrade_url ) . '" target="_blank">', '</a>' );
10+
echo wp_kses_post( $content );
11+
?>
12+
</div>
13+
<div class="locked-form-block">
14+
<div class="fz-form-group mb-24">
15+
<label class="form-label"><?php esc_html_e( 'The OpenRouter account API key:', 'feedzy-rss-feeds' ); ?></label>
16+
<div class="help-text pb-8">
17+
<?php
18+
// phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment
19+
echo wp_kses_post( sprintf( __( 'Get your OpenRouter API key from <a href="%1$s" target="_blank">%2$s</a>', 'feedzy-rss-feeds' ), esc_url( 'https://openrouter.ai/keys' ), __( 'OpenRouter API keys', 'feedzy-rss-feeds' ) ) );
20+
?>
21+
</div>
22+
<input type="password" class="form-control" placeholder="<?php echo esc_attr( __( 'API key', 'feedzy-rss-feeds' ) ); ?>"/>
23+
</div>
24+
<div class="fz-form-group">
25+
<label class="form-label"><?php esc_html_e( 'The OpenRouter model:', 'feedzy-rss-feeds' ); ?></label>
26+
<div class="help-text pb-8">
27+
<?php
28+
// phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment
29+
echo wp_kses_post( sprintf( __( 'OpenRouter API models <a href="%1$s" target="_blank">%2$s</a>', 'feedzy-rss-feeds' ), esc_url( 'https://openrouter.ai/models' ), __( 'Pricing', 'feedzy-rss-feeds' ) ) );
30+
?>
31+
</div>
32+
<div class="fz-input-group">
33+
<div class="fz-input-group-left">
34+
<input type="text" class="form-control" placeholder="<?php echo esc_attr( __( 'Model', 'feedzy-rss-feeds' ) ); ?>"/>
35+
<div class="help-text"><?php esc_html_e( 'API Status: Invalid | Last check: Never', 'feedzy-rss-feeds' ); ?></div>
36+
</div>
37+
<div class="fz-input-group-right">
38+
<div class="fz-input-group-btn">
39+
<button type="button" class="btn btn-outline-primary disabled"><?php esc_html_e( 'Validate connection', 'feedzy-rss-feeds' ); ?></button>
40+
</div>
41+
</div>
42+
</div>
43+
</div>
44+
</div>
45+
</div>
46+
</div>

js/ActionPopup/SortableItem.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,49 @@ const SortableItem = ({ propRef, loopIndex, item }) => {
165165
}
166166

167167
if ( 'chat_gpt_rewrite' === item.id ) {
168+
let defaultProvider = 'openai';
169+
let providerLicenseStatus = false;
170+
if ( feedzyData.apiLicenseStatus.openaiStatus ) {
171+
defaultProvider = 'openai';
172+
} else if ( feedzyData.apiLicenseStatus.openRouterStatus ) {
173+
defaultProvider = 'openrouter';
174+
}
175+
176+
let selectedProvider = item.data.aiProvider || defaultProvider;
177+
if ( 'openai' === selectedProvider ) {
178+
providerLicenseStatus = feedzyData.apiLicenseStatus.openaiStatus;
179+
} else if ( 'openrouter' === selectedProvider ) {
180+
providerLicenseStatus = feedzyData.apiLicenseStatus.openRouterStatus;
181+
}
168182
return(
169183
<li className="fz-action-control fz-chat-cpt-action" data-counter={counter}>
170184
<div className="fz-action-event">
171-
{feedzyData.isPro && (feedzyData.isBusinessPlan || feedzyData.isAgencyPlan) && !feedzyData.apiLicenseStatus.openaiStatus && (feedzyData.isHighPrivileges ? <span className="error-message">{__( 'Invalid API Key', 'feedzy-rss-feeds' )} <ExternalLink href="admin.php?page=feedzy-integration&tab=openai"><Icon icon={external} size={16} fill="#F00"/></ExternalLink></span> : <span className="error-message">{__( 'Invalid API Key, Please contact the administrator', 'feedzy-rss-feeds' )}</span> )}
185+
{feedzyData.isPro && (feedzyData.isBusinessPlan || feedzyData.isAgencyPlan) && !providerLicenseStatus && (feedzyData.isHighPrivileges ? <span className="error-message">{__( 'Invalid API Key', 'feedzy-rss-feeds' )} <ExternalLink href={`admin.php?page=feedzy-integration&tab=${item.data.aiProvider || defaultProvider}`}><Icon icon={external} size={16} fill="#F00"/></ExternalLink></span> : <span className="error-message">{__( 'Invalid API Key, Please contact the administrator', 'feedzy-rss-feeds' )}</span> )}
172186
<PanelBody title={ __( 'Rewrite with AI', 'feedzy-rss-feeds' ) } icon={ DragHandle } initialOpen={ false }>
173187
<PanelRow>
174188
<UpgradeNotice higherPlanNotice={!feedzyData.isBusinessPlan && !feedzyData.isAgencyPlan} utmCampaign="action-paraphrase-chatgpt"/>
189+
<BaseControl
190+
__nextHasNoMarginBottom
191+
className="mb-20"
192+
>
193+
<SelectControl
194+
__nextHasNoMarginBottom
195+
label={__('Choose an AI Provider', 'feedzy-rss-feeds')}
196+
value={ selectedProvider }
197+
options={[
198+
{
199+
label: __('OpenAI', 'feedzy-rss-feeds'),
200+
value: 'openai'
201+
},
202+
{
203+
label: __('OpenRouter', 'feedzy-rss-feeds'),
204+
value: 'openrouter'
205+
},
206+
]}
207+
onChange={ ( currentValue ) => propRef.onChangeHandler( { 'index': loopIndex, 'aiProvider': currentValue ?? '' } ) }
208+
disabled={!feedzyData.isPro}
209+
/>
210+
</BaseControl>
175211
<BaseControl
176212
__nextHasNoMarginBottom
177213
>
@@ -187,12 +223,27 @@ const SortableItem = ({ propRef, loopIndex, item }) => {
187223
}
188224
value={ item.data.ChatGPT ? unescape(item.data.ChatGPT.replaceAll('&#039;', '\'')) : '' }
189225
onChange={ ( currentValue ) => propRef.onChangeHandler( { 'index': loopIndex, 'ChatGPT': currentValue ?? '' } ) }
190-
disabled={!feedzyData.isPro || !feedzyData.apiLicenseStatus.openaiStatus}
226+
disabled={!feedzyData.isPro || !providerLicenseStatus}
191227
/>
192228
<div className="fz-prompt-button">
193-
<Button variant="secondary" onClick={ () => propRef.updatePromptText( { 'index': loopIndex, 'type': 'summarize' } ) }>{ __( 'Summarize', 'feedzy-rss-feeds' ) }</Button>
194-
<Button variant="secondary" onClick={ () => propRef.updatePromptText( { 'index': loopIndex, 'type': 'paraphase' } ) }>{ __( 'Paraphase', 'feedzy-rss-feeds' ) }</Button>
195-
<Button variant="secondary" onClick={ () => propRef.updatePromptText( { 'index': loopIndex, 'type': 'change_tone' } ) }>{ __( 'Change tone', 'feedzy-rss-feeds' ) }</Button>
229+
<Button
230+
variant="secondary"
231+
onClick={ () => propRef.updatePromptText( { 'index': loopIndex, 'type': 'summarize' } ) }
232+
disabled={!feedzyData.isPro || !providerLicenseStatus}
233+
>{ __( 'Summarize', 'feedzy-rss-feeds' ) }
234+
</Button>
235+
<Button
236+
variant="secondary"
237+
onClick={ () => propRef.updatePromptText( { 'index': loopIndex, 'type': 'paraphase' } ) }
238+
disabled={!feedzyData.isPro || !providerLicenseStatus}
239+
>{ __( 'Paraphase', 'feedzy-rss-feeds' ) }
240+
</Button>
241+
<Button
242+
variant="secondary"
243+
onClick={ () => propRef.updatePromptText( { 'index': loopIndex, 'type': 'change_tone' } ) }
244+
disabled={!feedzyData.isPro || !providerLicenseStatus}
245+
>{ __( 'Change tone', 'feedzy-rss-feeds' ) }
246+
</Button>
196247
</div>
197248
</BaseControl>
198249
</PanelRow>

js/feedzy-setting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jQuery( function( $ ) {
6464
$( ':input' ).change(function () {
6565
unsaved = true;
6666
});
67-
$( '#feedzy-settings-submit, #check_wordai_api, #check_spinnerchief_api, #check_aws_api, #check_openai_api' ).on( 'click', function() {
67+
$( '#feedzy-settings-submit, #check_wordai_api, #check_spinnerchief_api, #check_aws_api, #check_openai_api, #check_openrouter_api' ).on( 'click', function() {
6868
unsaved = false;
6969
} );
7070
window.addEventListener( 'beforeunload', function( e ) {

0 commit comments

Comments
 (0)