oont-contents/plugins/xc-woo-google-cloud-print/includes/woo_printnode.php
2025-02-08 15:10:23 +01:00

97 lines
No EOL
2.9 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
/**
* XC Woo PrintNode Class.
*
* @class XC_WOO_PrintNode
* @version 3.2
*/
if (!class_exists("XC_WOO_PrintNode")):
class XC_WOO_PrintNode {
public function __construct(){
if('yes' == get_option('xc_woo_cloud_use_printnode')){
add_filter("xc_woo_cloud_print_settings_sections", array($this,"printnode_section"),10,1);
add_filter( 'xc_woo_cloud_print_sections_settings', array($this, 'printnode_section_options'), 10, 3 );
add_filter('plugin_action_links_' . XC_WOO_CLOUD_BASE_NAME, array( $this, 'add_action_links' ));
}
}
public function printnode_section($sections){
$new_sections =array( 'printnode' => __("PrintNode", XC_WOO_CLOUD));
$sections = $this->insertValueAtPosition($sections, $new_sections, 1);
return $sections;
}
public function printnode_section_options( $settings,$current_section,$id ){
$id = $id."_".$current_section;
if($current_section == 'printnode'){
$settings = array(
'section_title' => array(
'name' => __( 'PrintNode', XC_WOO_CLOUD ),
'type' => 'title',
'desc' => __('Configure PrintNode Here', XC_WOO_CLOUD),
'id' => $id.'_section_title'
),
'api' => array(
'name' => __( 'PrintNode API Key', XC_WOO_CLOUD ),
'type' => 'text',
'default' => '',
'desc' => __( 'PrintNode Api Key', XC_WOO_CLOUD ),
'id' => $id.'_api'
),
'section_end' => array(
'type' => 'sectionend',
'id' => $id.'_section_end'
)
);
}
return $settings;
}
public function insertValueAtPosition($arr, $insertedArray, $position) {
$i = 0;
$new_array=array();
foreach ($arr as $key => $value) {
if ($i == $position) {
foreach ($insertedArray as $ikey => $ivalue) {
$new_array[$ikey] = $ivalue;
}
}
$new_array[$key] = $value;
$i++;
}
return $new_array;
}
public function get_printers(){
$api_key = get_option('xc_woo_cloud_printnode_api');
if(!empty($api_key)){
try{
$credentials = new PrintNode\Credentials();
$credentials->setApiKey($api_key);
$request = new PrintNode\Request($credentials);
$printers = $request->getPrinters();
return $printers;
}catch(Exception $e){
return new WP_Error( 'broke', __( "Invalid PrintNode API Key", XC_WOO_CLOUD ) );
}
}
return new WP_Error( 'broke', __( "Invalid PrintNode API Key", XC_WOO_CLOUD ) );
}
public function add_action_links($links)
{
$mylinks = array(
'<a href="' . admin_url('admin.php?page=wc-settings&tab=xc_woo_cloud') . '">Cloud Print Options</a>'
);
return array_merge($mylinks, $links);
}
}
global $xc_woo_printnode;
$xc_woo_printnode = new XC_WOO_PrintNode();
endif;