Created
March 15, 2021 20:11
-
-
Save arelidev/d92e6880f8fe4bcc00bbf056d235fb1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PhpOffice\PhpWord\IOFactory; | |
use PhpOffice\PhpWord\PhpWord; | |
use PhpOffice\PhpWord\Shared\Converter; | |
use PhpOffice\PhpWord\Shared\Html; | |
add_action( 'wp_enqueue_scripts', 'vpresearch_theme_enqueue_styles' ); | |
function vpresearch_theme_enqueue_styles() { | |
// Enqueue parent stylesheet | |
wp_enqueue_style( 'child-style', get_stylesheet_uri(), | |
array( 'parenthandle' ), | |
wp_get_theme()->get( 'Version' ) // this only works if you have Version in the style header | |
); | |
// Enqueue child stylesheet | |
wp_enqueue_style( 'corral-style', get_stylesheet_uri() ); | |
if ( is_page_template( 'template-corral-vue.php' ) ) { | |
// Local development | |
// wp_register_script( 'vpr-corral-vue-app', get_home_url( 1 ) . ':8080/js/app.js', array(), false, true ); | |
// Production build | |
wp_enqueue_script( 'vpr-corral-vue-app', get_stylesheet_directory_uri() . '/corral-vue/dist/js/app.js', array(), filemtime( get_template_directory() . '/corral-vue/dist/app.js' ), true ); | |
$object = array( | |
'baseUrl' => get_site_url(), | |
'response' => json_decode( vpr_get_corral() ) | |
); | |
wp_localize_script( 'vpr-corral-vue-app', 'wp_data', $object ); | |
wp_enqueue_script( 'vpr-corral-vue-app' ); | |
} | |
} | |
/** | |
* Get all facilities with resources | |
* | |
* @return bool|string | |
*/ | |
function vpr_get_corral() { | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, 'https://ep2dev.orsc.utexas.edu/services/api/v1/core-facility/facilities' ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' ); | |
curl_setopt( $ch, CURLOPT_USERPWD, 'vpr01' . ':' . 'sTeputrupenu26Stakur88epEyanUth8' ); | |
$result = curl_exec( $ch ); | |
if ( curl_errno( $ch ) ) { | |
echo 'Error:' . curl_error( $ch ); | |
} | |
curl_close( $ch ); | |
return $result; | |
} | |
add_action( 'rest_api_init', 'vpr_get_corral_api_endpoints' ); | |
function vpr_get_corral_api_endpoints() { | |
register_rest_route( | |
'corral-api', | |
'/export', | |
array( | |
array( | |
'methods' => WP_REST_Server::ALLMETHODS, | |
'callback' => 'export_file', | |
'args' => array( | |
'data' => array( | |
'default' => null, | |
), | |
), | |
), | |
) | |
); | |
} | |
/** | |
* Generate and save file for download | |
* | |
* @param $request | |
* | |
* @return false|string | |
*/ | |
function export_file( $request ) { | |
require_once( get_stylesheet_directory() . '/vendor/autoload.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
global $wp_filesystem; | |
// Get posted data | |
$method = $request->get_param( 'method' ); | |
$content = $request->get_param( 'editor' ); | |
$itemsArray = json_decode( $request->get_param( 'itemsArray' ) ); | |
$upload_dir = wp_upload_dir(); | |
$dir = trailingslashit( $upload_dir['basedir'] ) . 'text-tool'; | |
$subdir = "/tmp/"; | |
$filename = "download-" . time(); | |
$filepath = get_home_url( 1 ) . "/wp-content/uploads/sites/" . get_current_blog_id() . "/text-tool/tmp/"; | |
if ( ! is_file( $dir . $subdir . $filename ) ) { | |
WP_Filesystem(); | |
// Create main folder within upload if not exist | |
if ( ! $wp_filesystem->is_dir( $dir ) ) { | |
$wp_filesystem->mkdir( $dir ); | |
} | |
// Create a sub-folder in my new folder if not exist | |
if ( ! $wp_filesystem->is_dir( $dir . $subdir ) ) { | |
$wp_filesystem->mkdir( $dir . $subdir ); | |
} | |
// Delete similar files | |
// foreach ( glob( $dir . "/tmp/download-*.*" ) as $filename ) { | |
// unlink( $filename ); | |
// } | |
} | |
switch ( $method ) { | |
case ( 'word' ): | |
try { | |
// Initialize PHPWord | |
$phpWord = new PhpWord(); | |
$phpWordHTML = new Html(); | |
// Set default font size | |
$phpWord->setDefaultFontName( "Helvetica" ); | |
$phpWord->setDefaultFontSize( 12 ); | |
$phpWord->setDefaultParagraphStyle( | |
array( | |
'spaceAfter' => Converter::pointToTwip( 12 ), | |
'spacing' => 80, | |
) | |
); | |
// Create a new page section | |
$section = $phpWord->addSection(); | |
// Add content to page section | |
$phpWordHTML->addHtml( $section, $content ); | |
// Save file and set permission to 0644 | |
$filename = $filename . ".docx"; | |
$objWriter = IOFactory::createWriter( $phpWord ); | |
$objWriter->save( $dir . $subdir . $filename ); | |
$response = array( | |
'wp_upload_dir' => $upload_dir, | |
'filepath' => $filepath . $filename, | |
'status' => 'success' | |
); | |
} catch ( \PhpOffice\PhpWord\Exception\Exception $exception ) { | |
$response = $exception; | |
} | |
break; | |
case( 'excel' ): | |
$data = []; | |
foreach ( $itemsArray as $item ) : | |
$data[] = [ $item->name, preg_replace( '/<[^>]*>/', '', $item->description ) ]; | |
foreach ( $item->resources as $resource ) : | |
$data[] = [ $resource->name, preg_replace( '/<[^>]*>/', '', $resource->description ) ]; | |
endforeach; | |
// Add separator between facilities | |
$data[] = ['']; | |
endforeach; | |
$filename = $filename . ".xlsx"; | |
SimpleXLSXGen::fromArray( $data )->saveAs( $dir . $subdir . $filename ); | |
$response = array( | |
'wp_upload_dir' => $upload_dir, | |
'filepath' => $filepath . $filename, | |
'status' => 'success' | |
); | |
break; | |
default: | |
$response = true; | |
} | |
return json_encode( $response ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment