'application/json'];if($connection)$headers['Authorization']='Bearer '.$connection['connection_id'].'.'.$connection['secret'];
$response=wp_remote_post('https://yooty.app/api/wordpress/connection.php',['headers'=>$headers,'body'=>wp_json_encode(array_merge($data,['action'=>$action])),'timeout'=>90,'redirection'=>0,'sslverify'=>true,'limit_response_size'=>1000000]);
if(is_wp_error($response))throw new RuntimeException('Yooty could not be reached. Please retry.');
$body=json_decode(wp_remote_retrieve_body($response),true);if(!is_array($body)||empty($body['success']))throw new RuntimeException(is_array($body)?esc_html((string)($body['error']??'Request failed.')):'Yooty returned an unreadable response.');return $body;
}
function yooty_wp_connection_state(array $connection): array {
try{return yooty_wp_request('status',[],$connection);}catch(RuntimeException $e){
if(!in_array($e->getMessage(),['Connection not found.','Connection expired or disconnected.','Invalid connection credential.','Invalid connection.'],true))throw $e;
return ['status'=>'disconnected'];
}
}
add_action('admin_menu',static function(){add_options_page(__('Yooty widgets','yooty-widgets'),__('Yooty widgets','yooty-widgets'),'manage_options','yooty-growth-widgets','yooty_growth_settings');});
add_filter('plugin_action_links_'.plugin_basename(__DIR__.'/yooty-growth-widgets.php'),static function($links){array_unshift($links,''.esc_html__('Settings','yooty-widgets').'');return $links;});
add_action('admin_enqueue_scripts',static function($hook){if($hook!=='settings_page_yooty-growth-widgets')return;
wp_enqueue_script('yooty-widget-icons',plugins_url('widget-icons.js',__FILE__),[],'0.6.0-icons1',true);
wp_enqueue_script('yooty-widget-copy',plugins_url('widget-copy.js',__FILE__),[],'0.6.0-measurement1',true);wp_enqueue_script('yooty-widget-locale',plugins_url('widget-locale.js',__FILE__),['yooty-widget-copy'],'0.6.0',true);wp_enqueue_script('yooty-widget-fields',plugins_url('widget-fields.js',__FILE__),['yooty-widget-locale'],'0.6.0',true);wp_enqueue_style('yooty-widget-fields',plugins_url('widget-fields.css',__FILE__),[],'0.6.0');wp_enqueue_script('yooty-widget-admin',plugins_url('admin.js',__FILE__),['yooty-widget-fields','yooty-widget-icons'],'0.6.0-measurement1',true);wp_add_inline_script('yooty-widget-admin','window.YootyWordPressAdmin='.wp_json_encode(['ajax'=>admin_url('admin-ajax.php'),'nonce'=>wp_create_nonce('yooty_widgets_admin')]).';','before');wp_enqueue_style('yooty-widget-admin',plugins_url('admin.css',__FILE__),[],'0.6.0-icons1');
});
function yooty_growth_settings(): void {
if(!current_user_can('manage_options'))return;
echo '
'.esc_html__('Yooty widgets','yooty-widgets').'
'.esc_html__('Connect your business to edit its shared widget settings here. Changes are saved in Yooty and apply wherever that widget is installed.','yooty-widgets').'